How to play MIDI on the iPhone?

怎甘沉沦 提交于 2019-11-30 03:58:00

I've written a tutorial on how to play MIDI using CoreMIDI through an AUGraph here.

FYI for those going down this road: AVMIDIPlayer was introduced in iOS 8. Seems to work well on device, sim not so much.

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")

lukebuehler

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)

https://developer.apple.com/library/mac/#documentation/MusicAudio/Conceptual/CoreAudioOverview/ARoadmaptoCommonTasks/ARoadmaptoCommonTasks.html#//apple_ref/doc/uid/TP40003577-CH6-SW13

About the sampler audio unit see: Simple embeddable MidiSynth for iOS?

Plumenator

I found this library that is available for licensing. Hope it helps someone else.

http://www.crimsontech.jp/eng/softsynth/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!