[英]AVPlayerLayer stop play when enter background mode
I setup AVPlayerLayer with :我设置了 AVPlayerLayer :
audioPlayer = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:fileName]];
avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:audioPlayer];
[avPlayerLayer setFrame:self.view.bounds];
[[self.view layer] addSublayer:avPlayerLayer];
[audioPlayer play];
In the AppDelegete i add :在 AppDelegete 我添加:
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
Now in UIViewController Class i add:现在在 UIViewController 类中我添加:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
if (audioPlayer.rate == 0.0) {
[avPlayerLayer.player play];
} else {
[avPlayerLayer.player pause];
}
NSLog(@"4");
break;
case UIEventSubtypeRemoteControlPlay:
[avPlayerLayer.player play];
break;
case UIEventSubtypeRemoteControlPause:
[avPlayerLayer.player pause];
NSLog(@"3");
break;
case UIEventSubtypeRemoteControlNextTrack:
[self didPressNextSong:nil];
NSLog(@"2");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self didPressLastSong:nil];
NSLog(@"1");
break;
default:
break;
}
}
Now the problem is that when i move to background mode the song stop playing but when i press the play button in the remote control(the menu that popup when i press double click on the home button) the song keep playing from where it stoped.现在的问题是,当我进入后台模式时,歌曲停止播放,但是当我按下遥控器中的播放按钮(双击主页按钮时弹出的菜单)时,歌曲会从停止的地方继续播放。
Any idea why i have this issue?知道为什么我有这个问题吗?
See this solution:请参阅此解决方案:
How do I get my AVPlayer to play while app is in background? 如何让我的 AVPlayer 在应用程序处于后台时播放?
The key is in the UIBackgroundModes in the info plist.关键在信息 plist 的 UIBackgroundModes 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.