我有一个应用程序App plays audio
和App provides Voice over IP services
选项配置在.plist
。当应用程序在后台时启动AVAudioPlayer
该应用程序正在接收套接字事件(即使在后台)和关于事件,播放声音。当应用程序在前台时,一切正常。当应用程序在后台时,应用程序被触发,播放器的播放方法被调用,但没有声音。
我已经尝试了许多互联网示例代码,但没有任何工作。我试图在前台开始播放声音,然后在后台移动我的应用程序,它正在工作。
当应用程序在后台时开始播放声音时出现什么问题?
这里是我的代码处理事件时,我的应用程序触发背景:
NSError *error;
NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:@"HeadspinLong" withExtension:@"caf"];
if (audioPlayer != nil) return;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
audioPlayer.delegate=self;
if (error) {
NSLog(@"%@", [error localizedDescription]);
}
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive: YES error: &error];
audioSession.delegate=self;
if (error) {
NSLog(@"");
}
可能重复的[AVAudioPlayer不会在后台开始播放](http://stackoverflow.com/questions/16564298/avaudioplayer-doesnt-start-playing-in-background) – matt 2013-05-15 16:39:55