Just beginning coding in Swift with Swift Playgrounds on my iPad. To build my app I first need to know which midi devices are connected and then select the one I want to use. Here is my code. When I press Select, it doesn’t return any name even if I have two devices plugged in.
import SwiftUI
import CoreMIDI
struct ContentView: View {
@State private var selectedDevice: String = ""
@State private var deviceList: [String] = []
var body: some View {
VStack {
TextField("Selected Device", text: $selectedDevice)
.padding()
Button("Select Device") {
self.deviceList = getMIDIDeviceList()
}
.padding()
}
}
private func getMIDIDeviceList() -> [String] {
var deviceNames = [String]()
let numberOfDevices = MIDIGetNumberOfDevices()
for index in 0..<numberOfDevices {
let device = MIDIGetDevice(index)
var name: Unmanaged<CFString>?
MIDIObjectGetStringProperty(device, kMIDIPropertyName, &name)
if let name = name {
deviceNames.append(name.takeUnretainedValue() as String)
}
}
return deviceNames
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
If you click "Select" in your little app, the midi device list is already correctly build, you simply do not display it!
Display your list
You could add this to
ContentView
:Select from list
But you can also do it more elegantly, by getting the list first with a
task
modifier when the view is created, then feeding the list to aPicker
:Nice, but what about...
Select an actual midi device
You selected a name, but what you will really need is selecting a device. For this you will need
MIDIDeviceRef
again and again. We need to store this somewhere, so we create a mini-model for the deviced calledMidiDevice
, and feed the picker with the changed list: