- (void)viewDidLoad {
[super viewDidLoad];
[self setupVideoPlayback];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
if ([self isPlaying]) {
// stop the video playback
[self stopPlayback];
}
}
-(void)stopPlayback {
[self.player setRate:0];
self.player = nil;
}
-(BOOL)isPlaying {
if (self.player.currentItem && self.player.rate > 0) {
return YES;
}
return NO;
}
-(void)setupVideoPlayback {
NSURL *url = [[NSBundle mainBundle] URLForResource:self.videoFile withExtension:@"mp4"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
self.player = [AVPlayer playerWithPlayerItem:item];
[self.player seekToTime:kCMTimeZero];
[self.player play];
}
转载于:https://my.oschina.net/suyongchen/blog/652584