当我在声音播放时启动我的iPhone游戏时,播放的背景音乐或播客停止播放.我注意到其他游戏允许继续播放背景音频.
这怎么可能?我是否需要覆盖App Delegate中的方法?
我有一个使用CoreBluetooth背景模式的应用程序.当某个事件发生时,我需要发出警报声.一切都在前台工作正常,所有蓝牙功能在后台工作正常.我也有它工作的地方UILocalNotification在后台安排发出警报,但我不喜欢这些音量控制不足,所以想用AVAudioPlayer播放闹钟声音.
我已将背景模式添加audio到我的.plist文件中,但无法正常播放声音.
我正在使用单例类进行警报并初始化声音如下:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:soundName
ofType:@"caf"]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
player.numberOfLoops = -1;
[player setVolume:1.0];
Run Code Online (Sandbox Code Playgroud)
我开始这样的声音:
-(void)startAlert
{
playing = YES;
[player play];
if (vibrate)
[self vibratePattern];
}
Run Code Online (Sandbox Code Playgroud)
并用它来振动:
-(void)vibratePattern
{
if (vibrate && playing) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[self performSelector:@selector(vibratePattern) withObject:nil afterDelay:0.75];
}
}
Run Code Online (Sandbox Code Playgroud)
振动在背景中工作正常,但没有声音.如果我用它Systemsound来播放这样的声音,它播放正常(但没有音量控制):
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &_sound);
AudioServicesPlaySystemSound(_sound);
Run Code Online (Sandbox Code Playgroud)
那么AVAudioPlayer没有播放声音文件的原因是什么?
谢谢
编辑-------
当应用程序背景时,声音将播放.然而,在背景下开始播放是行不通的.