AVPlayer从iOS的7背景通知播放音频 [英] AVPlayer play audio from iOS 7 background notification
问题描述
我音频
,取
和远程通知
在 UIBackgroundModes
,我成功地接收与我通过后台(未激活)的应用程序的远程通知:
I have audio
, fetch
and remote-notification
set in UIBackgroundModes
and I successfully receive remote notifications with my app in the background (not active) via:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
我有我的下面(的UIApplication *)应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions - (BOOL)应用:
:
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);
}
而在 - (无效)申请:(的UIApplication *)应用didReceiveRemoteNotification:(NSDictionary的*)USERINFO fetchCompletionHandler:(无效(^)(UIBackgroundFetchResult))completionHandler
我有以下内容:
NSLog(@"Playing url: %@", filePath);
AVPlayerItem * currentItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:filePath]];
[self.audioPlayer replaceCurrentItemWithPlayerItem:currentItem];
[self.audioPlayer play];
我看到这个code通过的NSLog执行,但没有发出声音。实际上,如果该应用接收的要背景在几秒钟内通知,音频确实起到,或即第一次它得到一个通知音频播放,但从来没有以后。
I see this code execute via NSLog but no sound is produced. Actually, if the app receives a notification within a few seconds of going to the background, audio does play, or ie. the first time it gets a notification audio plays, but never after that.
在搭载iOS 7的应用程序可启动音频输出异步像这样,从背景,即得。之后它已经睡着了,不生产了一段时间的任何音频?
Can an app in iOS 7 initiate audio output asynchronously like this, from the background, ie. after it has been asleep and not produced any audio for some time?
推荐答案
您不能的启动的声音在背景中。在音频
后台模式允许你这样做的唯一的事情就是继续担任该应用从前台到后台去发声。
You cannot initiate audio in the background. The only thing the audio
background mode allows you to do is to continue producing sound as the app goes from the foreground to the background.
这是一个完全合理的规则。这将是可怕的如果它真的发生在后台运行任何应用程序可能会突然开始生产出来的声音时,它喜欢的设备,给用户的神秘化和烦恼!
And this is a perfectly reasonable rule. It would be terrible if any app that happened to be running in the background could suddenly start producing sound out of the device whenever it likes, to the mystification and annoyance of the user!
不过,如果你的应用程序能够接收远程事件,并且如果它产生的声音,以便它是远程事件目标,然后用音频
背景模式,它可以继续作为远程事件的目标,因此可以产生声音的背景下,只要没有其它的应用程序将成为远程事件目标在其间。
However, if your app is capable of receiving remote events, and if it has produced sound so that it is the remote event target, then, with audio
background mode, it can go on being the remote event target and thus can produce sound in the background, as long as no other app becomes the remote event target in the meantime.
的最可靠的方式来产生声音,而在该背景是由声音附加到本地通知
The most reliable way to produce a sound while in the background is by attaching the sound to a local notification.
这篇关于AVPlayer从iOS的7背景通知播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!