iOS background audio not playing

前端 未结 5 2141
悲哀的现实
悲哀的现实 2020-11-27 05:51

I have an app that uses CoreBluetooth background modes. When a certain event happens I need to play an alarm sound. Everything works fine in the foreground and

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 06:48

    Maybe you should make your app's audio session higher than others.

    Besides the process to setting background mode, you must set AudioSession correctly.

    Sometimes just doing this is not enough

    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
    

    because in Help document there is a discussion about setActive

    Discussion
    If another active audio session has higher priority than yours (for example, a phone call), and neither audio session allows mixing, attempting to activate your audio session fails. Deactivating your session will fail if any associated audio objects (such as queues, converters, players, or recorders) are currently running.
    

    So, setActive:withOptions:error: is needed. Just like this

    [audioSession setCategory :AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error]  
    

    That is you must make your app's audio session higher than others.

提交回复
热议问题