ios播放聲音中斷後台音樂的問題,ios播放中斷後台

來源:互聯網
上載者:User

ios播放聲音中斷後台音樂的問題,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];果然網易雲音樂也可以恢複了,看了蘋果官方的文章,上面有張圖

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.