iOS App播放完自己的音视频后,如何重新继续播放后台音乐
前提:用戶反饋聽歌的時(shí)候切到我們的APP,APP會(huì)讓歌曲暫停,體驗(yàn)不是很好
一般情況下我們播放自己的音視頻是獨(dú)占揚(yáng)聲器的:
[[AVAudioSession sharedInstance] setActive:YES error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
如何重新繼續(xù)播放后臺(tái)音樂:
在播放完自己的音視頻后,代碼設(shè)置
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
代碼意思是告訴AVAuduio管理器說(shuō):我不用Audio服務(wù)了,你喚醒其他需要Auduio服務(wù)的App
重新續(xù)播后臺(tái)音樂會(huì)卡頓UI
這個(gè)蘋果有說(shuō)明:
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.
翻譯過(guò)來(lái)就是:
將會(huì)話設(shè)置為活動(dòng)的或非活動(dòng)的。注意,激活音頻會(huì)話是一個(gè)同步(阻塞)操作。
因此,我們建議應(yīng)用程序不要從線程激活它們的會(huì)話,因?yàn)殚L(zhǎng)時(shí)間的阻塞操作將會(huì)導(dǎo)致問(wèn)題。
請(qǐng)注意,如果會(huì)話在運(yùn)行或之后被設(shè)置為非活動(dòng)狀態(tài),該方法將在iOS 8或之后的應(yīng)用程序中拋出異常
暫停I/O(例如:音頻隊(duì)列、播放器、錄音機(jī)、轉(zhuǎn)換器、遠(yuǎn)程I/Os等)
這個(gè)卡頓是來(lái)回切換模式太快,沒有辦法的,當(dāng)你來(lái)回執(zhí)行才會(huì)發(fā)現(xiàn)這個(gè)問(wèn)題,只能動(dòng)畫處理一下。
另外會(huì)拋出異常:
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.
解決辦法就是要先暫停或關(guān)閉自己的音視頻, 再setActive為NO
但是當(dāng)你這樣寫了之后發(fā)先還是拋出這個(gè)異常,這你就可以再打印一下這兩者的執(zhí)行順序,你會(huì)發(fā)現(xiàn),居然是先setActive為NO了。
我的辦法是寫一個(gè)動(dòng)畫,在動(dòng)畫執(zhí)行完成后再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];
});
}
}
另外值得注意的一點(diǎn)是,有些情況居然設(shè)置動(dòng)畫都還是先setActive為NO再執(zhí)行的播放器暫停。這令人費(fèi)解,怎么處理呢,既然
setActive為YES和setActive為NO是要成對(duì)出現(xiàn)的,那我就再設(shè)置一遍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是不處理的,后臺(tái)音樂停掉就停掉了!閑魚在切到魚塘有視頻顯示的時(shí)候就關(guān)掉后臺(tái)音樂了,再切到別的tab,它就沒續(xù)播。'毒'也是!
總結(jié)
以上是生活随笔為你收集整理的iOS App播放完自己的音视频后,如何重新继续播放后台音乐的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: rom区别于ram的特点有哪些
- 下一篇: Debian忘记密码修改