MPMoviePlayerController 和 AVAudioPlayer 音频混合故障

发布于 2024-10-15 01:52:17 字数 1355 浏览 2 评论 0原文

我正在为 iPhone 开发一个交互式故事书类型的应用程序,最近我遇到了一个与设备上的音频混合有关的令人沮丧的错误。

首先,我设置了一个音频会话。我将类别设置为 AVAudioSessionCategoryAmbient,然后初始化并播放我的 AVAudioPlayer 实例。现在,在后台播放音频时,我使用 MPMoviePlayerController 预加载要播放的视频,然后调用 prepareToPlay。我以这种方式预加载视频的原因是因为我需要它稍后在提示时以相当严格的时间立即播放。

在此配置中,音频/电影工作正常,它们混合并且不会互相干扰。然而,这个特定的音频会话类别不允许音频在设备锁定时继续播放,这是我真正需要的功能。因此,我被迫考虑不同的类别:AVAudioSessionCategoryPlayback

默认情况下,此类别不允许与其他音频混合,根据 Apple 文档。为了启用与其他音频的混合,我覆盖了相关类别:

OSStatus propertySetError = 0;
UInt32 setProperty = 1;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(setProperty), &setProperty);
assert(propertySetError == 0);

不幸的是,这解决了我在锁定时播放的问题,但引入了另一个问题:随着视频加载时出现轻微的卡顿,AVAudioPlayer音频会短暂中断。停顿很小,可能不到一秒,但足以破坏用户体验。我已阅读这篇相关文章,它使我能够使用AVAudioSessionCategoryAmbient,但不幸的是,相同的方法似乎不适用于新类别。

根据返回码,音频会话类别应用成功。有谁知道为什么使用此类别启用音频混合与环境类别提供的混合设施不同?

I'm developing an interactive storybook type application for the iPhone and I've recently encountered a frustrating bug concerning audio mixing on the device.

Firstly, I setup an audio session. I set the category to AVAudioSessionCategoryAmbient and then init and play my AVAudioPlayer instance. Now, in the background whilst the audio is playing I'm pre-loading a video to play using an MPMoviePlayerController followed by a call to prepareToPlay. The reason I pre-load the video this way is because I need it to play instantly later on cue with fairly strict timing.

In this configuration, the audio/movie works fine and they mix and do not interrupt each other. However, this particular audio session category does not permit audio to continue playing while the device is locked, a feature I really need. As a result I'm forced to consider a different category: AVAudioSessionCategoryPlayback.

By default this category does not permit mixing with other audio, according to the Apple docs. To enable mixing with other audio I am overriding the relevant category:

OSStatus propertySetError = 0;
UInt32 setProperty = 1;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(setProperty), &setProperty);
assert(propertySetError == 0);

Unfortunately, this solves my playing whilst locked issue but introduces another issue: the AVAudioPlayer audio is interrupted briefly as the video loads with a minor stutter. The stutter is small, perhaps less than a second but is enough to disrupt the user experience. I've read this related post which enabled me to pre-load the video with the AVAudioSessionCategoryAmbient, but unfortunately the same approach doesn't seem to work with the new category.

The audio session category is applied successfully, according to the return code. Does anyone know why enabling audio mixing with this category is not the same as the mixing facility provided by ambient category?

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

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

发布评论

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

评论(1

抽个烟儿 2024-10-22 01:52:17

我发现解决类似问题的最佳方法是使用较新的 AVPlayer (+1 @adam) 并将您的应用设置为启用背景音频并接收远程控制通知。 @MarquelV 在之后向我透露了这一点您从 iPod 应用程序播放音乐,同时仍在应用程序中接收远程控制事件吗?

如果您可以让后台正常工作,那么您应该可以在设备锁定时继续播放。哦,别忘了向 info.plist 添加键,这很容易做到,但不知道为什么它不起作用。

The best way I've found working a similar problem is to use the newer AVPlayer (+1 @adam) and set your app to enable background audio and receive remote control notifications. I was tipped off to this by @MarquelV following How can you play music from the iPod app while still receiving remote control events in your app?

If you can get backgrounding working properly, that should enable you to continue playing while the device is locked. Oh, and don't forget to add keys to info.plist, its easy to do and then have no idea why it isn't working.

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