1、如何播放视频
iOS提供了MPMoviePlayerController、MPMoviePlayerViewController两个类,可以用来轻松播放视频和网络流媒体\网络音频。
提示:网络音频同样使用此控制器播放。YouTobe就是用MPMoviePlayerController实现的。
MPMoviePlayerViewController只能全屏播放视频。
上述两个类都定义在了MediaPlayer框架中,必须手动添加:
2、MPMoviePlayerController支持的格式
MPMoviePlayerController
继承自NSObject
内部有个view可以展示视频内容
将该视图添加其他控制器的view上,即可显示视频内容
MPMoviePlayerController可以播放的视频格式包括:
H.264、MPEG-4等
支持的文件扩展名包括:avi,mkv,mov,m4v,mp4等
可以从苹果官网:http://support.apple.com/kb/HT1425下载一些用来测试的视频文件,文件都比较小
提示:MPMoviePlayerController并不支持所有的视频格式,如果要播放不支持的视频格式,需要借助第三方框架进行解码,如VLC
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的播放状态是通过通知中心监听的
例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#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:
例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#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
|