我在audio、fetch和remote-notification中设置了UIBackgroundModes,我成功地通过以下方式接收远程通知,后台是我的应用程序(非活动):
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler我的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中有以下内容:
self.audioPlayer = [[AVPlayer alloc] init];
NSError *sessionError = nil;
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive:YES error:&activationError];
if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&sessionError]) {
NSLog(@"[AppDelegate] Failed to setup audio session: %@", sessionError);
}在- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler中,我有以下内容:
NSLog(@"Playing url: %@", filePath);
AVPlayerItem * currentItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:filePath]];
[self.audioPlayer replaceCurrentItemWithPlayerItem:currentItem];
[self.audioPlayer play];我看到这段代码是通过NSLog执行的,但是没有产生声音。实际上,如果应用程序在到达后台的几秒钟内收到通知,音频就会播放。它第一次收到通知音频播放,但从来没有在那之后。
iOS 7中的应用程序可以像这样异步启动音频输出,从后台,即.在它已经睡了一段时间并且没有产生任何声音之后?
发布于 2014-01-07 04:37:09
您不能在后台启动音频。audio后台模式唯一允许您做的事情是,在应用程序从前台到后台时继续生成声音。
这是一条完全合理的规则。如果任何一个碰巧在后台运行的应用程序突然开始从设备中产生声音,不管它喜欢什么时候,用户都会感到困惑和烦恼,那就太糟糕了!
但是,如果您的应用程序能够接收远程事件,如果它已经发出声音,使其成为远程事件目标,那么,在audio背景模式下,它可以继续作为远程事件目标,从而可以在后台产生声音,只要同时没有其他应用程序成为远程事件目标。
在后台生成声音的最可靠方法是将声音附加到本地通知。
https://stackoverflow.com/questions/20962049
复制相似问题