I have audio
, fetch
and remote-notification
set in 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);
}
And in - (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 中的应用程序可以像这样从后台异步启动音频输出吗?在它进入睡眠状态并且有一段时间没有发出任何声音之后?