Iphone 在后台播放视频

Iphone 在后台播放视频,iphone,ios,objective-c,avplayer,avplayerlayer,Iphone,Ios,Objective C,Avplayer,Avplayerlayer,我看到一张便条,上面写着这个地址 注意:如果禁用电影中的视频曲目,或将AVPlayerLayer与其关联的AVPlayer分离,则将允许电影音频在后台继续播放。但是,这些更改必须在应用程序实际切换到后台之前生效 这个程序有效。但是在背景中,声音被切断了。我只想继续播放背景声音。如何在AVPlayerLayer和AVPlayer之间分离?您上面的代码还可以,但在转换到/从后台转换时,请执行以下操作: 当应用程序处于活动状态时:(您的avPlayerLayer必须是添加到视图中的avPlayer

我看到一张便条,上面写着这个地址

注意:如果禁用电影中的视频曲目,或将AVPlayerLayer与其关联的AVPlayer分离,则将允许电影音频在后台继续播放。但是,这些更改必须在应用程序实际切换到后台之前生效


这个程序有效。但是在背景中,声音被切断了。我只想继续播放背景声音。如何在AVPlayerLayer和AVPlayer之间分离?

您上面的代码还可以,但在转换到/从后台转换时,请执行以下操作:

  • 当应用程序处于活动状态时:(您的avPlayerLayer必须是添加到视图中的avPlayerLayer)

  • 当应用程序进入后台时:(您的avPlayerLayer必须是添加到视图中的avPlayerLayer)

另外,别忘了在plist的UIBackgroundModes中添加音频


干杯。

我想这正是我需要的!快速提问(可能是noobish):
applicationwillresignative
是一个
AppDelegate
方法,我所有的
AVPlayerLayer
代码都在一个单独的ViewController文件中。在AppDelegate中引用VideoViewController播放器的最佳方式是什么?将VideoVideoController导入AppDelegate,或者是否存在应用程序范围的“获取AVPlayer当前运行的内容”方法或其他方法?最佳实践是什么?您可以使用委托,也可以使用通知。(1) 有关委托的使用,请参阅此stackoverflow问题:。(2) 有关通知,请参阅以下内容:。我建议使用通知。(使用UIApplicationIDBecMeaActivification和UIApplicationWillResignActivification)。
   [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
   [self becomeFirstResponder];

   [[AVAudioSession sharedInstance] setDelegate: self];
   [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
   [[AVAudioSession sharedInstance] setActive: YES error: nil];



    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:AnUrl]];
   AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];


   AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
   avPlayerLayer.frame = self.view.layer.bounds;
   UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
   [newView.layer addSublayer:avPlayerLayer];
   [self.view addSubview:newView];

   [avPlayer play];
- (void)applicationDidBecomeActive:(UIApplication *)application {
    [avPlayerLayer playerLayerWithPlayer:avPlayer];

}
- (void)applicationWillResignActive:(UIApplication *)application {
    [avPlayerLayer playerLayerWithPlayer:nil];
}