Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AVPlayer从iOS 7后台通知播放音频_Ios_Iphone_Audio_Background - Fatal编程技术网

AVPlayer从iOS 7后台通知播放音频

AVPlayer从iOS 7后台通知播放音频,ios,iphone,audio,background,Ios,Iphone,Audio,Background,我在UIBackgroundModes中设置了audio、fetch和remote notification,我通过以下方式成功地在后台通过我的应用程序接收远程通知(未激活): 我的:-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项: self.audioPlayer = [[AVPlayer alloc] init]; NSError *sessionError = nil; NSError *activationErr

我在
UIBackgroundModes
中设置了
audio
fetch
remote notification
,我通过以下方式成功地在后台通过我的应用程序接收远程通知(未激活):

我的:
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项

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-didReceiveMemotentification:(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
background模式允许您做的唯一一件事就是在应用程序从前台转到后台时继续产生声音

这是一个非常合理的规则。如果碰巧在后台运行的任何应用程序能够在它喜欢的时候突然从设备中发出声音,这将是非常可怕的,这会让用户感到困惑和烦恼

但是,如果您的应用程序能够接收远程事件,并且如果它产生了声音,因此它是远程事件目标,那么,使用
音频
后台模式,它可以继续作为远程事件目标,因此可以在后台产生声音,只要同时没有其他应用程序成为远程事件目标


在后台发出声音的最可靠方法是将声音附加到本地通知。

谢谢。本地通知是否可以播放应用程序
NSDocumentDirectory
中的声音资源,或者仅在资源中编译?必须在应用程序包中,您可以看到原因:本地通知可能会在几天后发出,因此我们需要保证声音文件仍然存在。此外,它必须是系统警报类型的声音(即aiff/wav),因为不会为您带来任何声音解码资源。当然,声音必须很短。
            NSLog(@"Playing url: %@", filePath);

            AVPlayerItem * currentItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:filePath]];

            [self.audioPlayer replaceCurrentItemWithPlayerItem:currentItem];
            [self.audioPlayer play];