1、如何播放视频
iOS提供了MPMoviePlayerController、MPMoviePlayerViewController两个类,可以用来轻松播放视频和网络流媒体\网络音频。
提示:网络音频同样使用此控制器播放。YouTobe就是用MPMoviePlayerController实现的。
MPMoviePlayerViewController只能全屏播放视频。
上述两个类都定义在了MediaPlayer框架中,必须手动添加:
2、MPMoviePlayerController支持的格式
n MPMoviePlayerController
n 继承自NSObject
n 内部有个view可以展示视频内容
n 将该视图添加其他控制器的view上,即可显示视频内容
n MPMoviePlayerController可以播放的视频格式包括:
n H.264、MPEG-4等
n 支持的文件扩展名包括:avi,mkv,mov,m4v,mp4等
n 可以从苹果官网:http://support.apple.com/kb/HT1425下载一些用来测试的视频文件,文件都比较小
n 提示:MPMoviePlayerController并不支持所有的视频格式,如果要播放不支持的视频格式,需要借助第三方框架进行解码,如VLC
n https://github.com/videolan/vlc
3、MPMoviePlayerController的使用
加载视频资源(注意,如果url为nil同样可以加载):
NSAssert(self.url, @"URL不能为空");
[[MPMoviePlayerController alloc] initWithContentURL:self.url];
显示:
[self.view addSubview:self.moviePlayer.view];
通过设置AutoresizingMask属性可以在横竖屏转换时自动调整视图大小
播放:
[self.moviePlayer play];
全屏:
[self.moviePlayer setFullscreen:YES animated:YES];
【备注】MPMoviePlayerController的播放状态是通过通知中心监听的
例如:
#import "MJMoviePlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface MJMoviePlayerViewController ()
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end
@implementation MJMoviePlayerViewController
- (MPMoviePlayerController *)moviePlayer
{
if (!_moviePlayer) {
// 负责控制媒体播放的控制器
NSURL *url =
[[NSBundle mainBundle] URLForResource:@"promo_full.mp4" withExtension:nil];
_moviePlayer =
[[MPMoviePlayerController alloc] initWithContentURL:url];
_moviePlayer.view.frame = self.view.bounds;
_moviePlayer.view.autoresizingMask =
UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_moviePlayer.view];
}
return _moviePlayer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.moviePlayer play];
}
@end
运行结果:
4、常用监听通知事件
状态变化:
MPMoviePlayerPlaybackStateDidChangeNotification
播放结束:
MPMoviePlayerPlaybackDidFinishNotification
退出全屏:
MPMoviePlayerDidExitFullscreenNotification
截屏完成:
MPMoviePlayerThumbnailImageRequestDidFinishNotification
截屏方法:
-requestThumbnailImagesAtTimes:timeOption:
例如:
#import "MJMoviePlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface MJMoviePlayerViewController ()
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end
@implementation MJMoviePlayerViewController
- (MPMoviePlayerController *)moviePlayer
{
if (!_moviePlayer) {
// 负责控制媒体播放的控制器
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.movieURL];
_moviePlayer.view.frame = self.view.bounds;
_moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_moviePlayer.view];
}
return _moviePlayer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.moviePlayer play];
[self addNotification];
}
- (void)viewDidAppear:(BOOL)animated
{
self.moviePlayer.fullscreen = YES;
}
#pragma mark - 添加通知
- (void)addNotification
{
// 1. 添加播放状态的监听
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(stateChanged) name:
MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
// 2. 播放完成
[nc addObserver:self selector:@selector(finished) name:
MPMoviePlayerPlaybackDidFinishNotification object:nil];
// 3. 全屏
[nc addObserver:self selector:@selector(finished) name:
MPMoviePlayerDidExitFullscreenNotification object:nil];
// 4. 截屏完成通知
[nc addObserver:self selector:@selector(captureFinished:) name:
MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
// 数组中有多少时间,就通知几次
// MPMovieTimeOptionExact 精确
// MPMovieTimeOptionNearestKeyFrame 大概
[self.moviePlayer requestThumbnailImagesAtTimes:@[@(5.0), @(20.0)] timeOption:MPMovieTimeOptionNearestKeyFrame];
}
- (void)captureFinished:(NSNotification *)notification
{
NSLog(@"%@", notification);
[self.delegate moviePlayerDidCapturedWithImage:notification.userInfo[MPMoviePlayerThumbnailImageKey]];
}
- (void)finished
{
// 1. 删除通知监听
[[NSNotificationCenter defaultCenter] removeObserver:self];
// 2. 返回上级窗体
// 谁申请,谁释放!
[self.delegate moviePlayerDidFinished];
NSLog(@"完成");
}
/**
MPMoviePlaybackStateStopped, 停止
MPMoviePlaybackStatePlaying, 播放
MPMoviePlaybackStatePaused, 暂停
MPMoviePlaybackStateInterrupted, 中断
MPMoviePlaybackStateSeekingForward, 下一个
MPMoviePlaybackStateSeekingBackward 前一个
*/
- (void)stateChanged
{
switch (self.moviePlayer.playbackState) {
case MPMoviePlaybackStatePlaying:
NSLog(@"播放");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暂停");
break;
case MPMoviePlaybackStateStopped:
// 执行[self.moviePlayer stop]或者前进后退不工作时会触发
NSLog(@"停止");
break;
default:
break;
}
}
@end