我有 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中的应用程序能否像这样从后台异步启动音频输出,即。在它已经睡着并且一段时间没有产生任何音频之后?
最佳答案
您不能在后台启动音频。唯一的audio
背景模式允许您做的是在应用程序从前台到后台时继续产生声音。
这是一个完全合理的规则。如果碰巧在后台运行的任何应用程序可以突然开始在设备中随时发出声音,这将是可怕的,这会让用户感到困惑和烦恼!
但是,如果您的应用程序能够接收远程事件,并且如果它产生了声音使其成为远程事件目标,那么使用 audio
背景模式,它可以继续作为远程事件目标,因此可以在后台发出声音,只要同时没有其他应用程序成为远程事件目标。
在后台产生声音的最可靠方法是将声音附加到本地通知。
关于ios - AVPlayer 从 iOS 7 后台通知播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20962049/