gpt4 book ai didi

ios - 如何在 iOS 中暂停后台播放器

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:33 25 4
gpt4 key购买 nike

我有这种情况:

在后台,默认的 iPhone 音频播放器(或任何其他音频播放器)正在播放一些音乐。在前台我的应用程序正在运行。然后,在某些情况下,我的应用程序必须播放音频文件(有点像 GPS 导航器)。我希望我的应用程序暂停后台播放器(闪避是不够的),播放它的文件,然后继续播放后台播放器。这可能吗?

谢谢,donescamillo@gmail.com

最佳答案

从 iOS 6 开始,您可以将您的 Audio Session 设置为事件状态,播放您的文件,然后使用 AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation 标志 选项停用您的 session 。确保在需要播放音频时设置不可混音类别,以便背景音频停止。

简单的步骤-

    // Configure audio session category and activate ready to output some audio
[[AVAudioSession sharedInstance] setActive:YES error:nil];
// Play some audio, then when completed deactivate the session and notify other sessions
[[AVAudioSession sharedInstance] setActive:NO withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

来自 Apple 的文档-

When passed in the flags parameter of the setActive:withOptions:error: instance method, indicates that when your audio session deactivates, other audio sessions that had been interrupted by your session can return to their active state. This flag is used only when deactivating your audio session; that is, when you pass a value of NO in the beActive parameter of the setActive:withOptions:error: instance method

还有-

Deactivating your session will fail if any associated audio objects (such as queues, converters, players or recorders) are currently running.

编辑:一个更详细的例子-

在应用程序生命周期开始时配置混合 Audio Session

    // deactivate session
BOOL success = [[AVAudioSession sharedInstance] setActive:NO error: nil];
if (!success) { NSLog(@"deactivationError"); }
// set audio session category AVAudioSessionCategoryPlayAndRecord
success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
if (!success) { NSLog(@"setCategoryError"); }
// set audio session mode to default
success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
if (!success) { NSLog(@"setModeError"); }
// activate audio session
success = [[AVAudioSession sharedInstance] setActive:YES error: nil];
if (!success) { NSLog(@"activationError"); }

当你的应用程序想要在没有任何背景音频播放的情况下输出音频时,首先像这样更改 Audio Session 类别

    // activate a non-mixable session
// set audio session category AVAudioSessionCategoryPlayAndRecord
BOOL success;
AVAudioSessionCategoryOptions AVAudioSessionCategoryOptionsNone = 0;
success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionsNone error:nil];
if (!success) { NSLog(@"setCategoryError"); }

// set audio session mode default
success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
if (!success) { NSLog(@"setModeError"); }

// activate audio session
success = [[AVAudioSession sharedInstance] setActive:YES error: nil];
if (!success) { NSLog(@"activationError"); }

// commence playing audio here...

当您的应用程序完成播放音频时,您可以停用 Audio Session

    // deactivate session and notify other sessions
// check and make sure all playing of audio is stopped before deactivating session...
BOOL success = [[AVAudioSession sharedInstance] setActive:NO withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error: nil];
if (!success) { NSLog(@"deactivationError"); }

我可以确认上面的代码是有效的,测试了音乐应用在运行 iOS 7.0.4iPhone 5 上播放,但是不能保证这一点,因为还有其他考虑因素,例如用户操作。例如,如果我插入耳机,音乐应用程序的背景音频会路由到耳机并继续播放,但如果我取下耳机,音乐应用程序产生的背景音频就会暂停。

更多信息请阅读 AVAudioSession 类引用

关于ios - 如何在 iOS 中暂停后台播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20993360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com