我制作了一个示例演示,在我的视图中播放了一个视频。
代码:
- (IBAction)playVideo:(id)sender
{
NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"sample_Video" ofType:@"mp4"]];
self.player = [AVPlayer playerWithURL:videoURL];
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.videoView.bounds;
[self.videoView.layer addSublayer:playerLayer];
[self.player play];
self.slider.hidden = NO;
self.playButton.enabled = NO;
self.pauseButton.enabled = YES;
}
-(IBAction)pauseVideo:(id)sender
{
[self.player pause];
self.playButton.enabled = YES;
self.pauseButton.enabled = NO;
}
我的问题是当我点击播放我的视频播放时。 当我点击暂停时,我的视频暂停了。
但是当我再次点击播放时,它又开始了。 我想恢复功能。
答案 0 :(得分:3)
这是因为您正在重新初始化播放器。 做这个技巧
在.h中声明一个变量 BOOL isResumed = false;
现在将您的代码更新为此
-(IBAction)playVideo:(id)sender {
if(isResumed) {
[self.player play];
self.pauseButton.enabled = YES;
}
else{
NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"sample_Video" ofType:@"mp4"]];
self.player = [AVPlayer playerWithURL:videoURL];
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.videoView.bounds;
[self.videoView.layer addSublayer:playerLayer];
[self.player play];
self.slider.hidden = NO;
self.playButton.enabled = NO;
self.pauseButton.enabled = YES;
}
}
-(IBAction)pauseVideo:(id)sender {
isResumed = true;
[self.player pause];
self.playButton.enabled = YES;
self.pauseButton.enabled = NO;
}
答案 1 :(得分:1)
这是因为在你的playVideo方法中你正在创建AVPlayer的新实例。添加'if'条件以检查您是否已经拥有AVPlayer然后只需恢复它。在playVideo方法的开头添加此代码:
public modalResponse: Observable<boolean> = new Observable((observer) => { });
public saveChanges() { ///OPEN MODAL AFTER CANDEAC
this.openSaveChangeModal();
return this.modalResponse.take(1);
}
canDeactivate(component: CheckoutComponent){
if(this.nav)
return true;
else{
component.saveChanges(); // Opens modal
return component.modalResponse.take(1);
}
}
// Modal save changes
public openSaveChangeModal() {
jQuery('#deactModal').modal();
}
public acceptNavigate(){
this.modalResponse = new Observable((observer) => {observer.next(true); });
jQuery('#deactModal').modal('hide');
}
public cancelNavigate(){
this.modalResponse = new Observable((observer) => { observer.next(false); });
jQuery('#deactModal').modal('hide');
}