V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
kera0a
V2EX  ›  iDev

AVAudioSession setActive 暂停与恢复背景音乐会 block ui,有大大能分享下解决方法么?

  •  
  •   kera0a · 2017-04-07 12:54:35 +08:00 · 7015 次点击
    这是一个创建于 2548 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的程序需要播放视频,视频播放时,需要暂停背景音乐,结束或划出可视区域暂停需要恢复背景音乐。

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

    用这个代码确实可行,但会卡住 UI 一下。

    尝试了一些方法, StackOverflow 上也碰到一些人提过,但是都没解决方法

    其中一个是将 UI 先改变完,再调用。让用户感知不到,但我的应用是个列表视频,用户很容易感知到。

    另外一个是个写 xamarin 的哥们的解决方法,将调用放到异步线程执行,这个哥们说可以解决这个问题,并写了一篇博客,拜读后自己尝试用 OBJC 实现,但无论是哪种异步执行,都会卡住 UI 。

    有没有大神能分享下自己的解决方案么? 先非常非常感谢一下!!!

    第 1 条附言  ·  2017-04-07 15:41:34 +08:00

    自己折腾了一下,解决了。 Apple 文档有写

    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.
    
    2 条回复    2020-09-28 17:04:46 +08:00
    QingFengMingYue
        1
    QingFengMingYue  
       2020-09-28 16:53:43 +08:00
    楼主好!我遇到了和你一样的问题,参考 StackOverflow 我把这句话 setActive 放在后台线程,UI 还是会卡一下。想请教一下你当时是怎么解决的?
    kera0a
        2
    kera0a  
    OP
       2020-09-28 17:04:46 +08:00 via iPhone
    @QingFengMingYue
    你这样试试看

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    sleep(1);
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:0 error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    sleep(1);
    [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
    });
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5652 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:31 · PVG 14:31 · LAX 23:31 · JFK 02:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.