视频
来源:互联网 发布:java读写分离 编辑:程序博客网 时间:2024/04/27 19:04
// 视频播放格式:
// .mp4 .mov .m4v .m2v .3gp .3g2
//视频播放使用#import <MediaPlayer/MediaPlayer.h>这个框架 不仅可以播放本地视频 也可以直接播放网络视频
//*****MPMoviePlayerController 播放视频的类 上面播放视频的view 需要给它坐标 还需要添加在 某个视图上
#import "ViewController.h"
//使用视频播放需要导入视频框架<MediaPlayer/MediaPlayer.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
//如果想使用AVAssetImageGenerator获得截图必须导入AVFoundation
//AVAsset通过URL就可以获得视频里面的资源 不仅是可以获得本地的 也可以获得网络的
//AVAssetImageGenerator 通过它就可以得到视频里面的图片(截图)
@interface ViewController ()
{
MPMoviePlayerController *moviePlayer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor = [UIColor blueColor];
// moviePlayer 可以通过通知来检测到视频播放的状态MPMoviePlayerPlaybackStateDidChangeNotification
// 写一个通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeState:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
NSString *path = [[NSBundle mainBundle]pathForResource:@"微电影《青春代码》——IT男为爱做黑客 青春无悔" ofType:@"mov"];
UIImage *image = [self thumbnailImage:path];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
/*
//播放视频的类
NSString *path = [[NSBundle mainBundle]pathForResource:@"微电影《青春代码》——IT男为爱做黑客 青春无悔" ofType:@"mov"];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
moviePlayer.view.frame = CGRectMake(100, 100, 200, 200);
moviePlayer.view.backgroundColor = [UIColor orangeColor];
// 设置视频显示控件的样式
moviePlayer.controlStyle = MPMovieControlStyleNone;
// 设置视频是否循环播放 MPMovieRepeatModeNone不循环, MPMovieRepeatModeOne循环
moviePlayer.repeatMode = MPMovieRepeatModeOne;
// 设置视频显示控制样式
moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
[self.view addSubview:moviePlayer.view];//把视频播放的view添加到self的view上 还需要设置视频播放器上的 frame
// [moviePlayer prepareToPlay];
// 是否允许分享到airPlay
moviePlayer.allowsAirPlay = YES;
NSLog(@"是否使用了AirPlay%d ",moviePlayer.allowsAirPlay);
// 是否需要自动继续播放
moviePlayer.shouldAutoplay = YES;
// 设置视频全屏
moviePlayer.fullscreen = YES;
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer play];
NSLog(@"是否全屏%d",moviePlayer.isFullscreen);
*/
}
- (void)changeState:(NSNotification *)not{
NSLog(@"%@",not.object);
// 获取当前的播放时间
NSLog(@"视频播放的时间%f",moviePlayer.currentPlaybackTime);
// 通知会把视频播放的对象传给我们
MPMoviePlayerController *mp = not.object;
// playbackState只读的属性可以通过它来获取视频播放的状态
NSLog(@"%ld",mp.playbackState);
// typedef NS_ENUM(NSInteger, MPMoviePlaybackState) {
// MPMoviePlaybackStateStopped,停止
// MPMoviePlaybackStatePlaying,播放
// MPMoviePlaybackStatePaused,暂停
// MPMoviePlaybackStateInterrupted,中断
// MPMoviePlaybackStateSeekingForward,快进
// MPMoviePlaybackStateSeekingBackward快退
// };
switch (mp.playbackState) {
case MPMoviePlaybackStateStopped:
NSLog(@"停止");
break;
case MPMoviePlaybackStatePlaying:
NSLog(@"播放");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暂停");
break;
case MPMoviePlaybackStateInterrupted:
NSLog(@"中断");
break;
case MPMoviePlaybackStateSeekingForward:
NSLog(@"快进");
break;
case MPMoviePlaybackStateSeekingBackward:
NSLog(@"快退");
break;
default:
break;
}
// 通过moviePlayerControler获得截图的方法
// MPMovieTimeOptionNearestKeyFrame 获得时间截图
// MPMovieTimeOptionExact获得绝对的截图时间
[moviePlayer requestThumbnailImagesAtTimes:@[@(2),@(1)] timeOption:MPMovieTimeOptionNearestKeyFrame];
// 成功截取通知名字
// MPMoviePlayerThumbnailImageRequestDidFinishNotification
// 可以获取通知里面传来得内容图片的key
// MPMoviePlayerThumbnailImageKey
}
//FFNPEG VFL 转码的开源框架
- (UIImage *)thumbnailImage:(NSString *)path{
// 1.通过URL获得视频的资源
// 1>. AVAsset 2>.AVURLAsset
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:path]];
// AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
// 2. 初始化视频资源的持有者
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
// 3.开始截图
// <#(CMTime)#>包含了时间值和帧数
// 拷贝那个时间段的图片(包括了那一帧)
CGImageRef ref = [generator copyCGImageAtTime:CMTimeMake(1, 2) actualTime:nil error:nil];
// 把CGImageRef转换成UIImage的过程
UIImage *image = [UIImage imageWithCGImage:ref];
return image;
}
@end