在 iOS 中,如何根据多任务处理处理其他音频播放?

发布于 2024-09-28 11:21:42 字数 1195 浏览 0 评论 0原文

音频会话食谱中有关于“检查应用程序启动期间是否正在播放其他音频"。

我正在使用 MonoTouch(但请随意给出 Objective-C 答案,API 是相同的),因此该文档中的代码看起来像这样:

bool otherAudioPlaying = AudioSession.OtherAudioIsPlaying;
if(otherAudioPlaying)
    AudioSession.Category = AudioSessionCategory.AmbientSound;
else
    AudioSession.Category = AudioSessionCategory.SoloAmbientSound;

结果是,如果我的应用程序以 iPod 音乐播放启动, iPod 音乐将继续播放并阻止我使用硬件音频解码器(无论如何我都不需要)。如果我的应用程序启动时没有播放 iPod 音乐,我可以使用硬件解码器。

然后我可以使用 otherAudioPlaying 的值来确定是否开始播放我自己的音乐。

现在,这在不支持多任务处理的旧操作系统上运行得很好。但通过多任务处理,用户可以离开我的应用程序(将其置于后台),开始在 iPod 上播放音乐,然后重新进入我的应用程序。然后他们的 iPod 音乐将被静音,我的音乐将再次开始播放。

我希望发生的是,当我的应用程序返回前台时,让 iPod 音乐继续播放 - 即使当我的应用程序处于后台时我自己的音乐正在播放(并使用硬件解码器)。

(相反,如果我的应用程序在后台没有播放音乐,我想检测当它返回时是否可以开始播放音乐)。

我该怎么做? (最好不要突然切断我的音频,保持通常发生的美好淡出。)

编辑:我还应该指出,我正在主线程外完成所有音频工作。

In the Audio Session Cookbook there is a section on "Checking if Other Audio is Playing During App Launch".

I'm using MonoTouch (but feel free to give an Objective-C answer, the API is the same), so the code from that document looks something like this:

bool otherAudioPlaying = AudioSession.OtherAudioIsPlaying;
if(otherAudioPlaying)
    AudioSession.Category = AudioSessionCategory.AmbientSound;
else
    AudioSession.Category = AudioSessionCategory.SoloAmbientSound;

The upshot is that, if my app is started with iPod music playing, that iPod music will keep playing and prevent me from using the hardware audio decoder (which I don't need anyway). If there's no iPod music playing when my app starts, I can use the hardware decoder.

I can then use the value of otherAudioPlaying to determine whether or not to start playing my own music.

Now that works just fine on the older operating systems that don't support multitasking. But with multitasking the user can leave my app (backgrounding it), start music playing on the iPod, and re-enter my app. Then their iPod music will be silenced and my music will start playing again.

What I would like to have happen is have the iPod music continue playing when my application returns to the foreground - even if my own music was playing (and using the hardware decoder) when my application was backgrounded.

(And, in reverse - if my application was backgrounded with no music playing, I'd like to detect if I can start playing music when it returns).

How can I do this? (And preferably without abruptly cutting off my audio, maintaining the nice fade-out that usually happens.)

EDIT: I should also point out that I'm doing all my audio work off the main thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹淡然 2024-10-05 11:21:42

为什么不在应用程序委托多任务消息中实现它呢?在后台,暂停音频(如有必要,存储当前播放位置),并在应用程序恢复焦点时将其旋转回来。

只是为了澄清一点:通过重置音频播放器状态来响应您的委托中的 applicationDidEnterBackground: ,并通过按照您现在的方式设置它来响应 applicationWillEnterForeground: 。您还可以让设置音频播放的对象通过 NSNotificationCenter 响应 UIApplicationWillEnterBackgroundNotification/UIApplicationWillEnterForegroundNotification,这可能会更容易一些,但代价是从应用程序委托类中抽象出应用程序在后台执行的操作。

Why not just take care of implementing it in your app delegate multitasking messages? On background, suspend your audio (storing current playback position if necessary), and spin it back up when the application resumes focus.

Just to clarify a bit more: respond to applicationDidEnterBackground: in your delegate by resetting your audio player state, and respond to applicationWillEnterForeground: by setting it back up the way you do now. You could also have the object that sets up audio play respond to UIApplicationWillEnterBackgroundNotification/UIApplicationWillEnterForegroundNotification through NSNotificationCenter, that would probably be a bit easier at the expense of abstracting away what your app does exactly on background out of your app delegate class.

南七夏 2024-10-05 11:21:42

关于您对 refulgentis 答案的最后评论,您可以在 applicationWillResignActive 中暂停音乐,并为 applicationDidEnterBackground 保留状态保存操作和清理。这可能更好地避免后台过渡状态期间出现任何奇怪的行为。

Regarding your last comment on refulgentis's answer, you could pause your music in applicationWillResignActive and leave state saving operations and cleanup for applicationDidEnterBackground. This might be better for avoiding any odd behaviour during the background transition state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文