這個東西和以前的音頻播放差很少, 也是先須要導入系統框架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
|