バックグラウンドでビデオを再生しようとしています.非常に多くのチュートリアルに従いましたが、適切な結果が得られませんでした.そのためにAVPlayerを使用しています.しかし、バックグラウンドで音楽を再生したいので、AVPlayer から AVPlayerLayer をデタッチする必要があります。しかし、バックグラウンドで avPlayerLayer にアクセスするたびに、NULL が返されます。
これは私のコードです:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"sample"
withExtension:@"m4v"
subdirectory:nil];
avPlayerItem = [AVPlayerItem playerItemWithURL:url];
self.songPlayer = [AVPlayer playerWithPlayerItem:avPlayerItem];
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer: self.songPlayer];
self.avPlayerLayer.frame = self.view.layer.bounds;
UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
[newView.layer addSublayer:avPlayerLayer];
[self.view addSubview:newView];
[ self.songPlayer play];
// Do any additional setup after loading the view, typically from a nib.
}
iewController *vc=[[ViewController alloc]init];
NSLog(@"%@",vc.avPlayerLayer);
vc.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
ViewController *vc=[[ViewController alloc]init];
vc.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:vc.songPlayer];
}