I am trying to play a MIDI file using audiokit's MIDIPlayer. I have the following code:
import SwiftUI
import AudioKit
struct ContentView: View {
@StateObject var midi = MIDIWrapper()
var body: some View {
Button(action: {midi.play()}, label: {Text("play")})
}
}
class MIDIWrapper: ObservableObject {
let engine = AudioEngine()
var instrument = AppleSampler()
let player: MIDIPlayer
init() {
engine.output = instrument
try? engine.start()
player = MIDIPlayer(audioEngine: engine.avEngine, filename: "cdec.mid")
}
func play() -> () {
player.play()
}
}
When I run this on my iPhone 13 I get the following error:
AVAEInternal.h:109 [AVAudioSequencer.mm:121:-[AVAudioSequencer(AVAudioSequencer_Player) startAndReturnError:]: (impl->Start()): error -10852
MIDIPlayer.swift:play():121:Could not start the sequencer (MIDIPlayer.swift:play():121)
Could anyone tell me how I can fix this?
The filename should be without extension. The extension is added in the initializer of MIDIPlayer
. Thanks to Nick Culbertson from Moby Pixel for pointing this out.
For people looking to play MIDI files with Audiokit, I would also recommend looking at the AppleSequencer
class. Code examples can be found in the Audiokit Cookbook.