在后台播放AVAudioPlayer的一个实例 [英] Play one instance of AVAudioPlayer in the background
问题描述
我的应用程序中有几个AVAudioPlayer实例(在不同的类中)。我已经为一个实例添加了多任务功能,但现在所有音频都在后台播放。我在我的plist中添加了App播放音频键,并且:
I have several instances of AVAudioPlayer in my app (in separate classes). I have added multi-tasking capabilities to one instance but all audio now plays in the background. I added the 'App plays audio' key to my plist and:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
到我班上。如何将此代码仅定位到AVAudioPlayer的1个实例?
to my class. How can I target this code to only 1 instance of AVAudioPlayer?
推荐答案
我能找到的唯一方法是暂停特定的AVAudioPlayer实例:
The only way around this I could find was the pause the specific AVAudioPlayer instance in:
applicationWillResignActive
我将实例的创建移到了appdelegate,然后通过以下方式访问:
I moved the creation of the instance to the appdelegate and then accessed it via:
appDelegate = [[UIApplication sharedApplication] delegate]
在我班上。可能有更好的方法来做到这一点,但这是我能找到的最快最简单的方法。
in my class. There is probably a better way to do this but this was the quickest and simplest I could find.
这篇关于在后台播放AVAudioPlayer的一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!