Reputation: 1682
As far as I know, there is not native or third party library available to play MIDI on the iPhone. But there seem to be quite a few apps that do exactly that. What are they using? Any clues?
Upvotes: 10
Views: 18412
Reputation: 10326
This worked for me for playing midi file on iPhone:
import AVFoundation
class MidiPlayer: NSObject {
static let shared = MidiPlayer()
var musicPlayer: MusicPlayer?
var sequence: MusicSequence?
func play(file: String) {
guard let midiFile = Bundle.main.url(forResource: file, withExtension: "mid") else {
return
}
NewMusicPlayer(&musicPlayer)
NewMusicSequence(&sequence)
if let musicPlayer = musicPlayer, let sequence = sequence {
MusicSequenceFileLoad(sequence, midiFile as CFURL, .midiType, MusicSequenceLoadFlags())
MusicPlayerSetSequence(musicPlayer, sequence)
MusicPlayerStart(musicPlayer)
}
}
func stop() {
if let musicPlayer = musicPlayer {
MusicPlayerStop(musicPlayer)
}
}
}
and then MidiPlayer.shared.play(file: "midifile")
Upvotes: 2
Reputation: 1434
FYI for those going down this road: AVMIDIPlayer was introduced in iOS 8. Seems to work well on device, sim not so much.
Upvotes: 6
Reputation: 4081
Check out the MusicPlayer class. Combined with the AUSampler audio unit available since iOS 5.0 you can build a MIDI player quite easily. (The link is OS X, but it applies for iOS as well)
About the sampler audio unit see: Simple embeddable MidiSynth for iOS?
Upvotes: 1