The background music is interrupted during ios playback.
Today, I encountered a problem where the background music was interrupted during ios playback. In my app, if AVAudioSession was called to play the audio, QQ music in the background could not be recovered occasionally, netease cloud music cannot resume playing once. After studying AVAudioSession, I called [audioSession setActive: NO error: & err]. There is another method.
/* Set the session active or inactive. Note that activating an audio session is a synchronous (blocking) operation.
Therefore, we recommend that applications not activate their session from a thread where a long blocking operation will be problematic.
Note that this method will throw an exception in apps linked on or after iOS 8 if the session is set inactive while it has running or
Paused I/O (e.g. audio queues, players, recorders, converters, remote I/OS, etc .).
*/
-(BOOL) setActive :( BOOL) active error :( NSError **) outError;
-(BOOL) setActive :( BOOL) active witexceptions :( AVAudioSessionSetActiveOptions) options error :( NSError **) outError NS_AVAILABLE_IOS (6_0 );
AVAudioSessionSetActiveOptions
/* Options for use when calling setActive: witexceptions: error:
AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation --
Using Y an interrupted app that the interruption has ended and it may resume playback. Only valid on
Session deactivation .*/
Typedef NS_OPTIONS (NSUInteger, AVAudioSessionSetActiveOptions)
{
Avaudiosessionsetactiveoption=yothersondeactivation = 1
} NS_AVAILABLE_IOS (6_0 );
The meaning of the comment is: "The interrupted program has ended and playback can be resumed .... ",
It seems that the playing sound may cause occasional failure to recover QQ music in the background, while Netease cloud music cannot resume playing once.
Then change it to [[AVAudioSession sharedInstance] setActive: NO witexceptions: avaudiosessionsetactiveoption=yothersondeactivation error: nil]. NetEase cloud music can be restored. After reading the official Apple article, there is a picture on it.