支持: .MP4、.mov、.mov、.m4v、.m2v、.3gp、.3g2
1.视频播放使用<MediaPlayer/MediaPlayer.h>不仅可以播放视频本地视频,也可以直接播放网络视频
2.MPMoviePlayerController 播放视频的类
3.(1)MPMoviePlayerController 上面 播放视频的 view
(2)需要给它坐标还需要添加在 某个视图上面
一.播放视频:
在ViewController.h里面导入播放视频的系统框架<MediaPlayer/MediaPlayer.h>
定义一个播放视频的全局变量
{
MPMoviePlayerController *moviePlayer;
}
初始化一个moviePlayer
先通过bundle目录找到视频资源
如:
NSString *path = [[NSBundle mainBundle]pathForResource:@"IMG_8435" ofType:@"m4v"];
UIImage *image = [self thumbnailImage:path];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
播放视频的类
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
还需要设置 moviePlayer.view 的frame
moviePlayer.view.frame = self.view.frame;
moviePlayer.view.backgroundColor = [UIColor grayColor];
设置视频显示控制的样式
moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
MPMovieRepeatModeNone 不循环
MPMovieRepeatModeOne 循环
设置视频 是否循环播放
1.moviePlayer.repeatMode = MPMovieRepeatModeOne;
MPMovieControlStyleNone: 没有自带的控制栏
MPMovieControlStyleEmbedded:(默认)
moviePlayer.repeatMode = MPMovieControlStyleEmbedded;
*****需要把视频播放器的view添加在 需要播放的视图上
[self.view addSubview:moviePlayer.view];
是否允许分享到airPlay
moviePlayer.allowsAirPlay = YES;
NSLog(@"是否 使用了 airPlay %d",moviePlayer.airPlayVideoActive);
如果播放的是网络视频 是否需要 自动继续播放
moviePlayer.shouldAutoplay = YES;
设置视频全屏
moviePlayer.fullscreen = YES;
[moviePlayer setFullscreen:YES animated:YES];
播放
[moviePlayer play];
加载的状态: MPMovieLoadState
NSLog(@"是否全屏 %d",moviePlayer.isFullscreen);
MPMovieLoadStateUnknown = 0, 未知的状态
MPMovieLoadStatePlayable = 1 << 0, 可以播放的状态
MPMovieLoadStatePlaythroughOK = 1 << 1, Playback will be
automatically started in this state when shouldAutoplay is YES 如果使用了
shouldAutoplay == YES 有可以播放的自动播放
MPMovieLoadStateStalled = 1 << 2, 没有网络数据 可以播放的时候
通知和截取图片:
截取图片:如果想使用AVAssetImageGenerator获得截图 必须导入AVFoundation框架
AVAsset 通过URL 获得视频的资源
AVAssetImageGenerator 可以得到视频里面的图片
moviePlayer 可以通过通知 来检测到视频播放的状态
MPMoviePlayerPlaybackStateDidChangeNotification
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeState:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
NSString *path = [[NSBundle mainBundle]pathForResource:@"IMG_8435" ofType:@"m4v"];
UIImage *image = [self thumbnailImage:path];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
- (void)changeState:(NSNotification *)not
{
NSLog(@"视频播放的当前时间%f",moviePlayer.currentPlaybackTime);
NSLog(@"%@",not.object);
可以获得整个视频播放的对象
MPMoviePlayerController *MP = not.object;
playbackState 只读的属性可以通过它 来获取到视频播放的状态
NSLog(@"%ld",MP.playbackState);
MPMoviePlaybackStateStopped 停止
MPMoviePlaybackStatePlaying 播放
MPMoviePlaybackStatePaused 暂停
MPMoviePlaybackStateInterrupted 中断
MPMoviePlaybackStateSeekingForward 快进
MPMoviePlaybackStateSeekingBackward 快退
switch (MP.playbackState) {
case MPMoviePlaybackStateStopped:
NSLog(@"停止");
break;
case MPMoviePlaybackStatePlaying:
NSLog(@"播放");
break;
case MPMoviePlaybackStateInterrupted:
NSLog(@"中断");
break;
case MPMoviePlaybackStateSeekingForward:
NSLog(@"快进");
break;
case MPMoviePlaybackStateSeekingBackward:
NSLog(@"快退");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暂停");
break;
default:
break;
}
1. 通过moviePlayerController 获得截图的方法
MPMovieTimeOptionNearestKeyFrame 获得相对时间的截图
MPMovieTimeOptionExact 获得绝对时间的截图
[moviePlayer requestThumbnailImagesAtTimes:@[@(1),@(5)] timeOption: MPMovieTimeOptionNearestKeyFrame];
检测截图成功的通知
MPMoviePlayerThumbnailImageRequestDidFinishNotification
可以获得 通知传过来内容的 图片的key MPMoviePlayerThumbnailImageKey
}
*******FFMPEG VFL 转码的开源框架
- (UIImage *)thumbnailImage:(NSString *)path
{
1. 通过URL获得网址里面视频的资源
AVAsset
AVURLAsset
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:path]];
2.初始化视频资源图片的持有者
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
3.开始截图
<(CMTime)>包含了 时间的值
帧数
copyCGImageAtTime 拷贝哪个时间段的图片(包括了哪一帧)
CGImageRef 是图片的数据、参数
CGImageRef ref = [generator copyCGImageAtTime:CMTimeMake(1, 2) actualTime:nil error:nil];
CGImageRef 转换成UIImage
UIImage *image = [UIImage imageWithCGImage:ref];
return image;
}