iOS App播放完自己的音視頻后,如何重新繼續(xù)播放后臺(tái)音樂

From: https://www.cnblogs.com/ljcgood66/p/12132973.html

前提:用戶反饋聽歌的時(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管理器說:我不用Audio服務(wù)了,你喚醒其他需要Auduio服務(wù)的App

重新續(xù)播后臺(tái)音樂會(huì)卡頓UI

這個(gè)蘋果有說明:

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.

翻譯過來就是:

將會(huì)話設(shè)置為活動(dòng)的或非活動(dòng)的。注意,激活音頻會(huì)話是一個(gè)同步(阻塞)操作。
因此,我們建議應(yīng)用程序不要從線程激活它們的會(huì)話,因?yàn)殚L(zhǎng)時(shí)間的阻塞操作將會(huì)導(dǎo)致問題。
請(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è)卡頓是來回切換模式太快,沒有辦法的,當(dāng)你來回執(zhí)行才會(huì)發(fā)現(xiàn)這個(gè)問題,只能動(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.

解決辦法就是要先暫?;蜿P(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ù)播。'毒'也是!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容