【发布时间】:2014-06-11 07:15:53
【问题描述】:
我有一个主要基于核心蓝牙的应用程序。 当发生特定事情时,应用程序会使用 Core Bluetooth 后台模式唤醒并触发警报,但是当应用程序不在前台时,我无法让警报工作。
我有一个 Alarm Singleton 类,它像这样初始化AVAudioPlayer:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:soundName
ofType:@"caf"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[self.player prepareToPlay];
self.player.numberOfLoops = -1;
[self.player setVolume:1.0];
NSLog(@"%@", self.player);
这是调用我的报警代码时调用的方法:
-(void)startAlert
{
NSLog(@"%s", __FUNCTION__);
playing = YES;
[self.player play];
NSLog(@"%i", self.player.playing);
if (vibrate) {
[self vibratePattern];
}
}
现在,当应用程序在前台时,self.player.playing 返回 1 但是当应用程序在后台时 self.player.playing 返回 0。为什么会这样?
正在调用所有代码,因此应用程序处于唤醒状态并正常运行。
使用AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
知道为什么这个声音不会播放吗?
谢谢
【问题讨论】:
-
你用设备测试过吗?
-
当然可以。我需要蓝牙设备才能正常工作。
-
在这个答案中有你需要的所有信息..stackoverflow.com/questions/22591421/…
-
这是我几周前的原始问题,但没有得到有效的答案:(
-
在重要代码中传递错误参数并检查返回值。我很确定 AudioSession 返回错误。
标签: ios objective-c audio background avaudioplayer