我有一個應用程序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