iOS App播放完本身的音視頻後,如何從新繼續播放後臺音樂

前提:用戶反饋聽歌的時候切到咱們的APP,APP會讓歌曲暫停,體驗不是很好

通常狀況下咱們播放本身的音視頻是獨佔揚聲器的:session

[[AVAudioSession sharedInstance] setActive:YES error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

 

如何從新繼續播放後臺音樂:

在播放完本身的音視頻後,代碼設置app

[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

代碼意思是告訴AVAuduio管理器說:我不用Audio服務了,你喚醒其餘須要Auduio服務的Appasync

 

從新續播後臺音樂會卡頓UI

這個蘋果有說明:動畫

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.ui

翻譯過來就是:this

將會話設置爲活動的或非活動的。注意,激活音頻會話是一個同步(阻塞)操做。
所以,咱們建議應用程序不要從線程激活它們的會話,由於長時間的阻塞操做將會致使問題。
請注意,若是會話在運行或以後被設置爲非活動狀態,該方法將在iOS 8或以後的應用程序中拋出異常
暫停I/O(例如:音頻隊列、播放器、錄音機、轉換器、遠程I/Os等)spa

這個卡頓是來回切換模式太快,沒有辦法的,當你來回執行纔會發現這個問題,只能動畫處理一下。線程

 

另外會拋出異常:

AVAudioSession.mm:997:-[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.

解決辦法就是要先暫停或關閉本身的音視頻, 再setActive爲NO翻譯

可是當你這樣寫了以後發先仍是拋出這個異常,這你就能夠再打印一下這二者的執行順序,你會發現,竟然是先setActive爲NO了。code

個人辦法是寫一個動畫,在動畫執行完成後再setActive爲NO,代碼以下:

-(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [UIView animateWithDuration:0.1 animations:^{ if (self.player) { [self.player pause]; } } completion:^(BOOL finished) { [DhAVAudioSessionManage changAVAudioSessionWithOptionsCanBackPlayTheMusic]; }]; }
+(void)changAVAudioSessionWithOptionsCanBackPlayTheMusic { NSLog(@"-category--%@",AVAudioSession.sharedInstance.category); if ( AVAudioSession.sharedInstance.category == AVAudioSessionCategoryPlayback) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; }); } }

 

另外值得注意的一點是,有些狀況竟然設置動畫都仍是先setActive爲NO再執行的播放器暫停。這使人費解,怎麼處理呢,既然

setActive爲YES和setActive爲NO是要成對出現的,那我就再設置一遍setActive爲YES再setActive爲NO,代碼以下:

+(void)changAVAudioSessionWithOptionsCanBackPlayTheMusic2 { NSLog(@"-category2--%@",AVAudioSession.sharedInstance.category); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSError *error = nil; //            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionModeMoviePlayback error:&error]; if ( error ) NSLog(@"%@", error.userInfo); [[AVAudioSession sharedInstance] setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionModeMoviePlayback error:&error]; if ( error ) NSLog(@"%@", error.userInfo); [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; }); }

這樣就不拋異常了。

 

其餘

我看有些APP是不處理的,後臺音樂停掉就停掉了!閒魚在切到魚塘有視頻顯示的時候就關掉後臺音樂了,再切到別的tab,它就沒續播。'毒'也是! 

相關文章
相關標籤/搜索