AVPlayer播放来自iOS 7后台通知的audio
我有在UIBackgroundModes
设置audio
, fetch
和remote-notification
,我成功地通过我的应用程序在后台(不活动)接收远程通知:
- (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执行,但没有声音产生。 实际上,如果应用程序在几秒钟后收到通知到背景,audio确实播放,或即。 第一次得到通知audio播放,但从来没有。
iOS 7中的应用程序是否可以像这样从后台asynchronous启动audio输出,即, 睡了一段时间没有产生任何audio?
您无法在后台启动audio。 audio
背景模式允许您做的唯一事情就是在应用程序从前景到背景时继续制作声音。
这是一个完全合理的规则。 如果任何碰巧在后台运行的应用程序可能会突然开始从设备中随时随地产生声音,那将是非常可怕的,对于用户的神秘和烦恼!
但是,如果您的应用程序能够接收远程事件,并且已经产生了声音以使其成为远程事件目标,那么在audio
背景模式下,它可以继续作为远程事件目标,从而可以在背景,只要没有其他的应用程序成为远程事件的目标在此期间。
在背景中产生声音的最可靠方法是将声音附加到本地通知。