前提:用戶反饋聽歌的時候切到我們的APP,APP會讓歌曲暫停,體驗不是很好
一般情況下我們播放自己的音視頻是獨占揚聲器的:
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
如何重新繼續播放后台音樂:
在播放完自己的音視頻后,代碼設置
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
代碼意思是告訴AVAuduio管理器說:我不用Audio服務了,你喚醒其他需要Auduio服務的App
重新續播后台音樂會卡頓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.
翻譯過來就是:
將會話設置為活動的或非活動的。注意,激活音頻會話是一個同步(阻塞)操作。
因此,我們建議應用程序不要從線程激活它們的會話,因為長時間的阻塞操作將會導致問題。
請注意,如果會話在運行或之后被設置為非活動狀態,該方法將在iOS 8或之后的應用程序中拋出異常
暫停I/O(例如:音頻隊列、播放器、錄音機、轉換器、遠程I/Os等)
這個卡頓是來回切換模式太快,沒有辦法的,當你來回執行才會發現這個問題,只能動畫處理一下。
另外會拋出異常:
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了。
我的辦法是寫一個動畫,在動畫執行完成后再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,它就沒續播。'毒'也是!