I'm using AVMIDIPlayer to play a MIDI file in Swift on iOS. This works for a number of times. After I've played the file several times, notes starts muting, until nothing sounds at all.
This seems to be a memory leak. Memory usage of the application grows each time you play a MIDI file, and there is no way to free this memory. The larger the soundfont, the worst the problem.
I submitted a bug report a month ago on this and haven't received any answer. There is also a post on Stack Overflow by another user on this.
Is there any workaround to this issue?
Here is a test case where you can reproduce the issue:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var soundBank : NSURL!
var midiPath : NSURL!
var midi : AVMIDIPlayer!
override func viewDidLoad() {
super.viewDidLoad()
self.soundBank = NSBundle.mainBundle().URLForResource("soundfonts/TimGM6mb", withExtension: "sf2")
self.midiPath = NSBundle.mainBundle().URLForResource("mozart", withExtension: "mid")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func playMidi(sender: UIButton) {
var error: NSError?
do {
self.midi = try AVMIDIPlayer(contentsOfURL: self.midiPath, soundBankURL: self.soundBank)
} catch let error1 as NSError {
error = error1
self.midi = nil
} catch {
NSLog("Exception creating AVMIDIPlayer")
}
if let e = error {
NSLog("Error creating AVMidiPlayer: \(e.localizedDescription)")
self.midi = nil
} else {
midi.prepareToPlay()
self.midi.play(nil)
}
}
}