ios播放声音中断后台音乐的问题

 

 今天遇到一个ios播放声音中断后台音乐的问题,在我的app中如果调用AVAudioSession 播放完声音,后台的qq音乐偶尔不能恢复,而网易云音乐一次都不能恢复播放,研究了一下AVAudioSession ,我之前调用[audioSession setActive:NO error:&err];还有一个方法

/* 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 withOptions:(AVAudioSessionSetActiveOptions)options error:(NSError **)outError NS_AVAILABLE_IOS(6_0);

而AVAudioSessionSetActiveOptions这个枚举

/*  options for use when calling setActive:withOptions:error: 

AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation -- 

Notify an interrupted app that the interruption has ended and it may resume playback. Only valid on 

session deactivation. */

typedef NS_OPTIONS(NSUInteger, AVAudioSessionSetActiveOptions)

{

AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation = 1

} NS_AVAILABLE_IOS(6_0);

注释的意思是:“通知中断程序中断已经结束,可以恢复播放。。。。“,

看来这里应该是造成播放声音导致后台的qq音乐偶尔不能恢复,而网易云音乐一次都不能恢复播放的地方

 

 之后改成调用   [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];果然网易云音乐也可以恢复了,看了苹果官方的文章,上面有张图

 

 

转载于:https://www.cnblogs.com/dlz1123/p/4498235.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值