这个东西和以前的音频播放差很少, 也是先须要导入系统框架MediaPlayer.framework 才能使用到MPMoviePlayerController 的文件中导入相应的头文件windows
初始化:这里就有些不同了MPMoviePlayerController是能够经过远程url初始化的, 例如:app
1
|
MPMoviePlayerController *moviePlayer = [ [ MPMoviePlayerController alloc]initWithContentURL:[
NSURL
urlWithString:@
"URL"
] ];
|
接下来是控制器样式设置:框架
1
|
moviePlayer.moviewControlMode = MPMovieControlModeHidden;
|
可使用下列样式:
MPMovieControlModeDefault 显示播放/暂停、音量和时间控制
MPMovieControlModeVolumeOnly 只显示音量控制
MPMovieControlModeHidden 没有控制器url
一般状况下, 咱们通常都是本身来定义视频播放器, 因此大多数仍是选择最后没有控制器的那个.spa
屏幕宽高比例:code
1
|
moviePlayer.scallingMode = MPMovieScallingModeNone;
|
MPMovieScallingModeNone 不作任何缩放
MPMovieScallingModeAspectFit 适应屏幕大小,保持宽高比
MPMovieScallingModeAspectFill 适应屏幕大小,保持宽高比,可裁剪
MPMovieScallingModeFill 充满屏幕,不保持宽高比视频
1
2
|
[ moviePlayer play ];
// 开始播放
[ moviePlayer stop ];
// 中止播放
|
注册一个通知 你的程序能够配置电影播放器在什么时候候发送通知,包括结束加载内容、技术播放、改变宽高比等。电影播放器会将事件发送到 Cocoa 的通知中心,你能够对其进行配置,指定将这些事件转发到你的应用程序的一个对象。要接收这些通知,须要使用 NSNotificationCenter 类,为电影播放器添加一个观察者(observer):server
1
2
3
4
5
6
|
NSNotificationCenter
* notificationCenter = [
NSNotificationCenter
defaultCenter];
[ notificationCenter addObserver:
self
selector:
@selector
(moviePlayerPreloadFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:moviePlayer ];
// 通知会发到你指定的委托类和目标方法。通知参数让你能够知道是哪一个事件触发了委托方法:
-(
void
)moviePlayerPreloadDidFinish:(
NSNotification
*)notification{
//添加你的处理代码
}
|
你会观察到如下通知:
MPMoviePlayerContentPreloadDidFinishNotification
当电影播放器结束对内容的预加载后发出。由于内容能够在仅加载了一部分的状况下播放,因此这个通知可能在已经播放后才发出。
MPMoviePlayerScallingModeDidChangedNotification
当用户改变了电影的缩放模式后发出。用户能够点触缩放图标,在全屏播放和窗口播放之间切换。
MPMoviePlayerPlaybackDidFinishNotification
当电影播放完毕或者用户按下了Done按钮后发出。对象
这里有个小实例:自定义视频之用手势来控制视频进度和音量大小blog
1
2
3
4
5
|
#import <MediaPlayer/MediaPlayer.h>
@interface
KKBMoviePlayerController : MPMoviePlayerController<UIGestureRecognizerDelegate>
@end
|
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#import "KKBMoviePlayerController.h"
#import "AppDelegate.h"
@interface
KKBMoviePlayerController(){
BOOL
_inFullScreen;
UIPanGestureRecognizer *_pan;
CGPoint _lastPoint;
BOOL
_startChange;
BOOL
_changeVolume;
}
@end
@implementation
KKBMoviePlayerController
- (
id
)initWithContentURL:(
NSURL
*)url{
self
=[
super
initWithContentURL:url];
if
(
self
) {
self
.view.backgroundColor = [UIColor clearColor];
self
.initialPlaybackTime = -1;
self
.endPlaybackTime = -1;
[
self
prepareToPlay];
[
self
play];
[[
NSNotificationCenter
defaultCenter] addObserver:
self
selector:
@selector
(enterFullScreen:)
name:MPMoviePlayerWillEnterFullscreenNotification
object:
nil
];
[[
NSNotificationCenter
defaultCenter] addObserver:
self
selector:
@selector
(leaveFullScreen:)
name:MPMoviePlayerWillExitFullscreenNotification
object:
nil
];
}
return
self
;
}
#pragma mark - full screen controller
- (
BOOL
)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return
YES
;
}
- (
void
)handlePan:(UIPanGestureRecognizer*)rec{
if
(_inFullScreen) {
if
(rec.state == UIGestureRecognizerStateBegan) {
_lastPoint = [rec locationInView:
self
.view];
}
else
if
(rec.state == UIGestureRecognizerStateChanged) {
CGPoint nowPoint = [rec locationInView:
self
.view];
if
(_startChange ==
NO
) {
if
(fabs(nowPoint.y - _lastPoint.y) > fabs(nowPoint.x - _lastPoint.x)) {
_changeVolume =
NO
;
}
else
{
_changeVolume =
YES
;
}
_startChange =
YES
;
}
else
{
if
(_changeVolume) {
//change volume
float
volume = [[MPMusicPlayerController applicationMusicPlayer] volume];
float
newVolume = volume;
if
(nowPoint.x == _lastPoint.x) {
}
else
{
if
(nowPoint.x < _lastPoint.x) {
newVolume += 0.01;
}
else
{
newVolume -= 0.01;
}
}
if
(newVolume < 0) {
newVolume = 0;
}
else
if
(newVolume > 1.0) {
newVolume = 1.0;
}
[[MPMusicPlayerController applicationMusicPlayer] setVolume:newVolume];
}
else
{
//change playback state
if
(
self
.playbackState != MPMoviePlaybackStateSeekingForward &&
self
.playbackState != MPMoviePlaybackStateSeekingBackward) {
if
(nowPoint.y == _lastPoint.y) {
}
else
{
if
(nowPoint.y < _lastPoint.y) {
[
self
beginSeekingForward];
}
else
{
[
self
beginSeekingBackward];
}
}
_lastPoint = nowPoint;
}
}
}
}
else
if
(rec.state == UIGestureRecognizerStateCancelled ||
rec.state == UIGestureRecognizerStateEnded ||
rec.state == UIGestureRecognizerStateFailed){
_startChange =
NO
;
[
self
endSeeking];
}
}
}
- (
void
)enterFullScreen:(
NSNotification
*)notification{
_inFullScreen =
YES
;
_pan = [[UIPanGestureRecognizer alloc] initWithTarget:
self
action:
@selector
(handlePan:)];
_pan.delegate =
self
;
[[[[UIApplication sharedApplication] windows] objectAtIndex:0] addGestureRecognizer:_pan];
}
- (
void
)leaveFullScreen:(
NSNotification
*)notification{
_inFullScreen =
NO
;
[[[[UIApplication sharedApplication] windows] objectAtIndex:0] removeGestureRecognizer:_pan];
}
@end
|