一、AVPlayer监听播放结束,实现循环播放
- 添加播放器播放结束监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidPlayToEndTime:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:nil];
- 在结束访法中seek视频到开始时间点
- (void)playerItemDidPlayToEndTime:(NSNotification *)notification {
[self.player seekToTime:kCMTimeZero]
}
二、AVPlayerLooper循环播放
原生方法,可完全替代方法一
NSURL *url = nil;
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
AVQueuePlayer *player = [AVQueuePlayer queuePlayerWithItems:@[playerItem]];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
AVPlayerLooper *playerLooper = [AVPlayerLooper playerLooperWithPlayer:player templateItem:playerItem];
[player play];
三、MPMoviePlayerController实现循环播放
self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"new" ofType:@"mp4"]];
self.player = [[MPMoviePlayerController alloc] initWithContentURL:self.url];
[self.view addSubview:self.player.view];
self.player.shouldAutoplay = YES;
self.player.repeatMode = MPMovieRepeatModeOne;
[self.player.view setFrame:self.view.bounds];
self.player.scalingMode = MPMovieScalingModeAspectFill;
[self.player play];
四、ijkplayer循环播放
使用ijkPlayerDemo中的IJKFFMoviePlayerController做测试,IJKFFMoviePlayerController本身没有循环播放代码,需要手动添加一下。
先在IJKFFMoviePlayerController中添加loop属性,为bool类型,然后在.m文件421行添加相关代码 ijkmp_set_loop
- (void)play
{
if (!_mediaPlayer)
return;
[self setScreenOn:_keepScreenOnWhilePlaying];
[self startHudTimer];
if (self.loop) {
ijkmp_set_loop(_mediaPlayer, INT_MAX);
} else {
ijkmp_set_loop(_mediaPlayer, 1);
}
ijkmp_start(_mediaPlayer);
}
使用方法
self.videoPlayer = [[IJKFFMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path] withOptions:options];
self.videoPlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.videoPlayer.view.frame = self.view.bounds;
self.videoPlayer.loop = YES;
self.videoPlayer.scalingMode = IJKMPMovieScalingModeAspectFill;
self.videoPlayer.shouldAutoplay = YES;
// self.containView.autoresizesSubviews = YES;
[self.view addSubview:self.videoPlayer.view];
[self.videoPlayer prepareToPlay];
[self.videoPlayer play];