我正在开发一个小应用程序,我希望手机从我的位置控制器播放一个事件的声音文件。问题是当应用程序处于后台模式时,AVAudioPlayer 不会启动音频文件,但会安排代码,获取 NSLog 输出。另一件事是它在模拟器中有效,但在手机上无效。
为什么不呢?
这是我播放文件的代码:
- (void)tryPlayTaleSound:(NSString*)fileName {
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
NSError *error;
backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
// Check to see if iPod music is already playing
//UInt32 propertySize = sizeof(otherMusicIsPlaying);
//AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &otherMusicIsPlaying);
// Play the music if no other music is playing and we aren't playing already
//if (otherMusicIsPlaying != 1 && !backgroundMusicPlaying) {
//[backgroundMusicPlayer prepareToPlay];
[backgroundMusicPlayer play];
backgroundMusicPlaying = YES;
//}
NSLog(@"%s - ", __FUNCTION__);
}
在日志中我得到了这个。
Apr 26 13:57:48 iPhone CommCenter[58] <Notice>: Client [com.apple.persistentconnection[dataaccessd,85]] is telling PDP context 0 to go active.
Apr 26 13:57:50 iPhone Herning100[1467] <Warning>: Playing audiofile - filename is Fil03
Apr 26 13:57:50 iPhone mediaserverd[39] <Warning>: 13:57:50.653 <AudioControl> WARNING translating CMSession error: -12985
Apr 26 13:57:50 iPhone mediaserverd[39] <Warning>: 13:57:50.656 <AudioControl> WARNING translating CMSession error: -12985
Apr 26 13:57:50 IPhone mediaserverd[39] <Error>: 13:57:50.734 <AudioControl> AudioQueue: Error -12985 from AudioSessionSetClientPlayState(1467)
Apr 26 13:57:50 iPhone Herning100[1467] <Warning>: -[AppDelegate tryPlayTaleSound:] -
Apr 26 13:57:50 iPhone com.apple.debugserver-199[1465] <Warning>: 9 +28.832083 sec [05b9/0303]: error: ::mach_vm_region_recurse ( task = 0x1603, address => 0xfffffffe, size => 0, nesting_depth => 1024, info => 0x2fd7fe48, infoCnt => 12) addr = 0xfffffffe err = (os/kern) invalid address (0x00000001)
Apr 26 13:57:51 iPhone com.apple.debugserver-199[1465] <Warning>: 10 +0.187961 sec [05b9/0303]: error: ::mach_vm_region_recurse ( task = 0x1603, address => 0xfffffffe, size => 0, nesting_depth => 1024, info => 0x2fd7fe48, infoCnt => 12) addr = 0xfffffffe err = (os/kern) invalid address (0x00000001)
如果我使用 AVAudioPlayer 或 AVAudioSession 有区别吗?
希望有人可以提供帮助。
/拉斯