本文主要是介绍iOS 视频播放 AVPlayer 循环播放 闪屏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题终于解决,选用AVPlayer,重置一下AVPlayerItem就不会出现闪屏现象,注册一个通知同样可以实现循环播放,而且AVPlayer可以自定义播放样式,给了我们更大的发挥空间,完全可以自己DIY播放器样式。并且AVPlayer完全可以实现两个视频窗口播放!
1.需要引入两个类库:
在.h中引入#import
<AVFoundation/AVFoundation.h>在.m中引入#import
<CoreMedia/CoreMedia.h>
代码如下:
- (void)viewDidLoad
{[super viewDidLoad];UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];rightBtn.frame = CGRectMake(50, 420, 180, 50);[rightBtn addTarget:self action:@selector(doRight) forControlEvents:UIControlEventTouchUpInside];[rightBtn setTitle:@"同时播放" forState:UIControlStateNormal];[self.view addSubview:rightBtn];NSString *filePath = [[NSBundle mainBundle] pathForResource:@"找朋友" ofType:@"mp4"];NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];self.player_0 = [AVPlayer playerWithPlayerItem:playerItem];AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player_0];playerLayer.frame = CGRectMake(0, 0, 400, 500);playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;[self.view.layer addSublayer:playerLayer];[_player_0 play];//注册通知[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(runLoopTheMovie:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];}- (void)doRight
{NSString *filePath = [[NSBundle mainBundle] pathForResource:@"字母歌" ofType:@"mp4"];NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];self.player_1 = [AVPlayer playerWithPlayerItem:playerItem];AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player_1];playerLayer.frame = CGRectMake(420, 0, 400, 500);playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;[self.view.layer addSublayer:playerLayer];[_player_1 play];}- (void)runLoopTheMovie:(NSNotification *)n{//注册的通知 可以自动把 AVPlayerItem 对象传过来,只要接收一下就OKAVPlayerItem * p = [n object];//关键代码 [p seekToTime:kCMTimeZero];[_player_0 play];NSLog(@"重播");
}
这篇关于iOS 视频播放 AVPlayer 循环播放 闪屏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!