在后台iOS应用中播放audio
我有一个应用程序主要基于核心蓝牙。 当某些具体事情发生时,应用程序被唤醒使用核心蓝牙背景模式,它会发出警报,但是当应用程序不在前台时,我无法使警报工作。
我有一个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
。 为什么会这样? 所有的代码被调用,所以应用程序是清醒和function。 使用AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
完美震动效果AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
任何想法为什么这个声音不会播放?
谢谢
我有一个应用程序也需要背景audio,但我的应用程序与应用程序后台模式“IP语音”,因为它需要有时logging。 我玩背景audio告诉应用程序单身我需要在后台播放audio:
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid; if([thePlayer play]){ newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; }
编辑:你必须调用[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
在您的应用程序进入背景之前。 在我的应用程序中,您同时开始播放,如果播放器可能在后台启动,则应该这样做:
- (void)applicationDidEnterBackground:(UIApplication *)application{ // You should retain newTaskId to check for background tasks and finish them newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; }
苹果公司在其文档中有一个很好的技术问答文章 (另请参阅播放和录制背景audio )。
我认为缺less的一件大事就是您没有在Xcode设置中激活audio背景模式 :
也许在警报方法中添加[self.player prepareToPlay]
是有帮助的。
苹果文档
从能力选项卡的背景模式部分启用audio支持
或者通过在应用程序的Info.plist文件中包含具有audio值的UIBackgroundModes键来启用此支持
我认为你有为应用程序指定的audio背景模式。 即使如此,我不确定您可以在后台将audio会话设置为活动状态。 你需要在进入后台之前激活它。 您可能还需要播放一些无声的audio来保持这种活动,但这似乎是不好的做法(可能会耗尽电池)。 看看通知文件,似乎有一种方法可以让本地通知播放一个包含在你的包中的audio样本,这似乎是你想要做的,所以也许这是要走的路。
尝试这个 :
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
链接
试试这个http://www.sagorin.org/ios-playing-audio-in-background-audio/
您需要启用您的应用程序以在后台处理audio中断(并终止中断)。 应用程序通过通知中心处理audio中断:
-
首先,向通知中心注册您的应用程序:
- (void) registerForMediaPlayerNotifications { [notificationCenter addObserver : self selector: @selector (handle_iPodLibraryChanged:) name: MPMediaLibraryDidChangeNotification object: musicPlayer]; [[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications]; }
-
现在当中断开始时保存播放器状态:
- (void) audioPlayerBeginInterruption: player { NSLog (@"Interrupted. The system has paused audio playback."); if (playing) { playing = NO; interruptedOnPlayback = YES; } }
-
重新激活audio会话并在中断结束时继续播放:
-(void) audioPlayerEndInterruption: player { NSLog (@"Interruption ended. Resuming audio playback."); [[AVAudioSession sharedInstance] setActive: YES error: nil]; if (interruptedOnPlayback) { [appSoundPlayer prepareToPlay]; [appSoundPlayer play]; playing = YES; interruptedOnPlayback = NO; } }
下面是苹果的示例代码,完整实现了您要实现的function: