[flutter_midi]Playing midi notes in background on iOS
I try to write simple app which playing random notes while device stays in background mode. As I can see this could be done by calling unmute method. But unfortunately it still mutes all notes as soon as app enters background mode. In generated XCode project all capabilities set correct.
I believe that implementation of unmute method may be the source of problem
case "unmute":
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}...
May be it should be
case "unmute":
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} ...
Make sure to call prepare midi as soon as the app comes back, use the life cycle states. It’s due to the midi engine in memory
Hello. I did some research on the problem. And I found it's not related to any of your code. It's in AudioUnitMIDISynth class you using. For some reason in background mode Audio Unit requires more frames per slice. I don't really understand what it is but it seems like adding following code at the end of augraphSetup method solve the problem for me
if let sampler = midisynthUnit {
var maxFramesPerSlice:AVAudioFrameCount = 4096
AudioUnitSetProperty(sampler,
kAudioUnitProperty_MaximumFramesPerSlice,
kAudioUnitScope_Global,
0,
&maxFramesPerSlice,
UInt32(MemoryLayout<AVAudioFrameCount>.size))
}
Awesome! That’s for the research 😎
This issue has been closed due to inactivity.