flutter_midi icon indicating copy to clipboard operation
flutter_midi copied to clipboard

[flutter_midi]Playing midi notes in background on iOS

Open mboGraf opened this issue 6 years ago • 4 comments

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)
        } ...

mboGraf avatar Jul 09 '19 21:07 mboGraf

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

rodydavis avatar Jul 10 '19 11:07 rodydavis

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

mboGraf avatar Sep 08 '19 10:09 mboGraf

Awesome! That’s for the research 😎

rodydavis avatar Sep 08 '19 12:09 rodydavis

This issue has been closed due to inactivity.

github-actions[bot] avatar Jan 31 '20 00:01 github-actions[bot]