zoukankan      html  css  js  c++  java
  • iOS App播放完自己的音视频后,如何重新继续播放后台音乐

    前提:用户反馈听歌的时候切到我们的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,它就没续播。'毒'也是! 

  • 相关阅读:
    Xshell 使用纪要
    矩阵求逆
    Ubuntu 增加新用户
    matlab 常用图像处理
    Surface Evolver 基本操作、使用指南和珍贵资料
    latex 裁剪图片
    Inkscape 输入希腊字母
    Pyton——int内部功能介绍
    python——登陆接口设计(循环方法)
    Python之三层菜单
  • 原文地址:https://www.cnblogs.com/ljcgood66/p/12132973.html
Copyright © 2011-2022 走看看