Skip to content

Instantly share code, notes, and snippets.

@genedelisa
Last active April 4, 2020 20:43

Revisions

  1. genedelisa revised this gist Sep 10, 2014. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions MidIPlayer
    Original file line number Diff line number Diff line change
    @@ -19,9 +19,14 @@ func playMIDIFile() {
    println("Error \(e.localizedDescription)")
    }
    self.mp.prepareToPlay()
    self.mp.play({
    println("midi done")
    })

    self.mp.play(nil)

    // there is a crash when you use a completion
    //self.mp.play({
    // println("midi done")
    //})


    // or
    // var completion:AVMIDIPlayerCompletionHandler = {println("done")}
    @@ -33,9 +38,7 @@ func toggleMIDIPlayer() {
    if mp.playing {
    mp.stop()
    } else {
    mp.play({
    println("midi done")
    })
    self.mp.play(nil)
    }
    }

  2. genedelisa revised this gist Aug 11, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion MidIPlayer
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@
    var soundbank:NSURL!
    var mp:AVMIDIPlayer!


    func playMIDIFile() {

    // Load a SoundFont or DLS file.
  3. genedelisa created this gist Aug 11, 2014.
    47 changes: 47 additions & 0 deletions MidIPlayer
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    /// soundbanks are either dls or sf2. see http://www.sf2midi.com/
    var soundbank:NSURL!
    var mp:AVMIDIPlayer!


    func playMIDIFile() {

    // Load a SoundFont or DLS file.
    self.soundbank = NSBundle.mainBundle().URLForResource("GeneralUser GS MuseScore v1.442", withExtension: "sf2")

    // a standard MIDI file.
    var contents:NSURL = NSBundle.mainBundle().URLForResource("ntbldmtn", withExtension: "mid")

    var error:NSError?
    self.mp = AVMIDIPlayer(contentsOfURL: contents, soundBankURL: soundbank, error: &error)
    if self.mp == nil {
    println("nil midi player")
    }
    if let e = error {
    println("Error \(e.localizedDescription)")
    }
    self.mp.prepareToPlay()
    self.mp.play({
    println("midi done")
    })

    // or
    // var completion:AVMIDIPlayerCompletionHandler = {println("done")}
    // mp.play(completion)

    }

    func toggleMIDIPlayer() {
    if mp.playing {
    mp.stop()
    } else {
    mp.play({
    println("midi done")
    })
    }
    }

    func stopMIDIPLayer() {
    if mp.playing {
    mp.stop()
    }
    }