您的位置:首页 > 产品设计 > 产品经理

ios——MPMoviePlayerController截取视频缩略图 播放视频又可以截取视频缩略图

2016-04-21 16:39 489 查看
#import <AVKit/AVKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) MPMoviePlayerController *moviePlayer;//视频播放控制器

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// NSURL *url = [NSURL URLWithString:@"http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4"];

self.view.backgroundColor = [UIColor whiteColor];

//播放
[self.moviePlayer play];

//添加通知
[self addNotification];

//获取缩略图
[self thumbnailImageRequest];
}

/**
* 取得本地文件路径
*
* @return 文件路径
*/
-(NSURL *)getFileUrl{
NSString *urlStr=[[NSBundle mainBundle] pathForResource:@"The New Look of OS X Yosemite.mp4" ofType:nil];
NSURL *url=[NSURL fileURLWithPath:urlStr];
return url;
}
/**
* 取得网络文件路径
*
* @return 文件路径
*/
-(NSURL *)getNetworkUrl{

NSString *urlStr = @"http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4";

urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:urlStr];

return url;
}
-(MPMoviePlayerController *)moviePlayer{

if (!_moviePlayer) {

NSURL *url = [self getNetworkUrl];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
_moviePlayer.view.frame = self.view.bounds;
_moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

[self.view addSubview:_moviePlayer.view];
}

return _moviePlayer;
}

/**
* 获取视频缩略图
*/
-(void)thumbnailImageRequest{
//获取13.0s、21.5s的缩略图
[self.moviePlayer requestThumbnailImagesAtTimes:@[@13.0,@21.5] timeOption:MPMovieTimeOptionNearestKeyFrame];
}
/**
* 添加通知监控媒体播放控制器状态
*/
-(void)addNotification{

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

[notificationCenter addObserver:self
selector:@selector(mediaPlayerPlaybackStateChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayer];

[notificationCenter
addObserver:self selector:@selector(mediaPlayerPlaybackFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];

[notificationCenter
addObserver:self
selector:@selector(mediaPlayerThumbnailRequestFinished:)
name:MPMoviePlayerThumbnailImageRequestDidFinishNotification
object:self.moviePlayer];
}

/**
* 播放状态改变,注意播放完成时的状态是暂停
*
* @param notification 通知对象
*/
-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
switch (self.moviePlayer.playbackState) {
case MPMoviePlaybackStatePlaying:
NSLog(@"正在播放...");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暂停播放.");
break;
case MPMoviePlaybackStateStopped:
NSLog(@"停止播放.");
break;
default:
NSLog(@"播放状态:%li",self.moviePlayer.playbackState);
break;
}
}

/**
* 播放完成
*
* @param notification 通知对象
*/
-(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
NSLog(@"播放完成.%li",self.moviePlayer.playbackState);
}

/**
* 缩略图请求完成,此方法每次截图成功都会调用一次
*
* @param notification 通知对象
*/
-(void)mediaPlayerThumbnailRequestFinished:(NSNotification *)notification{
NSLog(@"视频截图完成.");
UIImage *image=notification.userInfo[MPMoviePlayerThumbnailImageKey];
//保存图片到相册(首次调用会请求用户获得访问相册权限)
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

-(void)dealloc{
//移除所有通知监控
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


于于开始升级了ios9,不管是模拟器还是真机播放都是黑屏,郁闷了好久好久,后来打印:App Transport Security has
blocked a cleartext HTTP (http://) resource load since it is insecure.
Temporary exceptions can be configured via your app's Info.plist file.

在plist文件设置了允许网络请求不安全,就可以了,详细做法请看我的文章:适配一栏关于ios9 的变化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: