天天看點

iOS播放器開發之MPMoviePlayerController

MPMoviePlayerController是iOS中進行視訊播放開發的一個控制類,裡面涵蓋了視訊播放中大部分的需求功能,在使用這個架構時,需要導入頭檔案

<MediaPlayer/MediaPlayer.h>

MPMoviePlayerController具備一般的播放器控制功能,例如播放、暫停、停止等。但是MPMediaPlayerController自身并不是一個完整的視圖控制器,如果要在UI中展示視訊需要将view屬性添加到界面中。

擴充MPMoviePlayerViewController:其實MPMoviePlayerController如果不作為嵌入視訊來播放(例如在新聞中嵌入一個視訊),通常在播放時都是占滿一個螢幕的,特别是在iPhone、iTouch上。是以從iOS3.2以後蘋果也在思考既然MPMoviePlayerController在使用時通常都是将其視圖view添加到另外一個視圖控制器中作為子視圖,那麼何不直接建立一個控制器視圖内部建立一個MPMoviePlayerController屬性并且預設全屏播放,開發者在開發的時候直接使用這個視圖控制器。這個内部有一個MPMoviePlayerController的視圖控制器就是MPMoviePlayerViewController,它繼承于UIViewController。MPMoviePlayerViewController内部多了一個moviePlayer屬性和一個帶有url的初始化方法,同時它内部實作了一些作為模态視圖展示所特有的功能,例如預設是全屏模式展示、彈出後自動播放、作為模态視窗展示時如果點選“Done”按鈕會自動退出模态視窗等。

首先說一下建立誤區:

MPMoviePlayerController *movieplayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
           

如果我們這樣直接使用movieplayer,會發現黑屏了。

原因:因為現在大多數是arc環境,movieplayer不能是局部變量,一旦方法走完,就自動銷毀了,是以黑屏。需要設定為全局變量。

@property (strong, nonatomic) MPMoviePlayerController * movieplayer;


MPMoviePlayerController *movieplayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self. movieplayer= movieplayer;
           

這樣,movieplayer的建立以及設定走完就不會銷毀。

下面看一下裡面的方法和屬性吧

//視訊的url位址
@property (nonatomic, copy) NSURL *contentURL;

//顯示媒體和播放控件的視圖
@property (nonatomic, readonly) UIView *view;

//用于定制的視圖,始終顯示在電影内容後面
@property (nonatomic, readonly) UIView *backgroundView;

//播放器的目前播放狀态,枚舉定義如下:
typedef NS_ENUM(NSInteger, MPMoviePlaybackState) {
    MPMoviePlaybackStateStopped,//停止播放
    MPMoviePlaybackStatePlaying,//正在播放
    MPMoviePlaybackStatePaused,//暫停播放
    MPMoviePlaybackStateInterrupted,//中斷播放
    MPMoviePlaybackStateSeekingForward,//快進
    MPMoviePlaybackStateSeekingBackward//快退
};
@property (nonatomic, readonly) MPMoviePlaybackState playbackState;

//播放器的網絡緩存狀态,枚舉定義如下:
typedef NS_OPTIONS(NSUInteger, MPMovieLoadState) {
    MPMovieLoadStateUnknown        = ,//狀态未知
    MPMovieLoadStatePlayable       =  << ,//緩存資料足夠開始播放,但是視訊并沒有緩存完全
    MPMovieLoadStatePlaythroughOK  =  << , //已經緩存完成,如果設定了自動播放,這時會自動播放
    MPMovieLoadStateStalled        =  << , //資料緩存已經停止,播放将暫停
};
@property (nonatomic, readonly) MPMovieLoadState loadState;


//播放器風格,枚舉如下:
typedef NS_ENUM(NSInteger, MPMovieControlStyle) {
    MPMovieControlStyleNone,       // 無控制器
    MPMovieControlStyleEmbedded,   // 嵌入視訊風格
    MPMovieControlStyleFullscreen, // 全屏播放風格

    MPMovieControlStyleDefault = MPMovieControlStyleEmbedded
};
@property (nonatomic) MPMovieControlStyle controlStyle;

//播放器的循環模式,枚舉如下:
typedef NS_ENUM(NSInteger, MPMovieRepeatMode) {
    MPMovieRepeatModeNone,//播放結束後不循環
    MPMovieRepeatModeOne//循環
};
@property (nonatomic) MPMovieRepeatMode repeatMode;

//是否開啟自動播放,預設是yes
@property (nonatomic) BOOL shouldAutoplay;

//設定是否充滿螢幕,預設是no
@property (nonatomic, getter=isFullscreen) BOOL fullscreen;

//設定是否充滿螢幕,帶動畫效果
- (void)setFullscreen:(BOOL)fullscreen animated:(BOOL)animated;

//設定播放器的填充方式,枚舉定義如下:
typedef NS_ENUM(NSInteger, MPMovieScalingMode) {
    MPMovieScalingModeNone,       // 無縮放
    MPMovieScalingModeAspectFit,  // 适應大小模式
    MPMovieScalingModeAspectFill, // 充滿可視範圍,可能會被裁剪
    MPMovieScalingModeFill        // 縮放到充滿視圖
};
@property (nonatomic) MPMovieScalingMode scalingMode;

//傳回YES說明資料棧已經緩存好資料,傳回NO則沒有緩存好
@property (nonatomic, readonly) BOOL readyForDisplay

//下面是分類:
@interface MPMoviePlayerController (MPMovieProperties)

//資料檔案的格式,枚舉如下:
typedef NS_OPTIONS(NSUInteger, MPMovieMediaTypeMask) {
    MPMovieMediaTypeMaskNone  = ,//格式未知
    MPMovieMediaTypeMaskVideo =  << ,//音頻格式
    MPMovieMediaTypeMaskAudio =  << //視訊格式
};
@property (nonatomic, readonly) MPMovieMediaTypeMask movieMediaTypes

//視訊的資料類型,枚舉如下:
typedef NS_ENUM(NSInteger, MPMovieSourceType) {
    MPMovieSourceTypeUnknown,//類型未知
    MPMovieSourceTypeFile,     // 檔案類型
    MPMovieSourceTypeStreaming // 資料流
};
@property (nonatomic) MPMovieSourceType movieSourceType


//視訊檔案的時長
@property (nonatomic, readonly) NSTimeInterval duration


//緩存完成能夠播放的時長
@property (nonatomic, readonly) NSTimeInterval playableDuration

//視訊的原始大小
@property (nonatomic, readonly) CGSize naturalSize

//播放器開始播放的時間
@property (nonatomic) NSTimeInterval initialPlaybackTime


//播放器結束播放的時間
@property (nonatomic) NSTimeInterval endPlaybackTime;

//是否允許雲端播放,在ios5.0之後預設是yes
@property (nonatomic) BOOL allowsAirPlay;

//訓示電影播放器目前是否通過AirPlay播放視訊
@property (nonatomic, readonly, getter=isAirPlayVideoActive) BOOL airPlayVideoActive

//擷取視訊某一些時間點的縮略圖
typedef NS_ENUM(NSInteger, MPMovieTimeOption) {
    MPMovieTimeOptionNearestKeyFrame,//使用最近的關鍵幀生成縮略圖
    MPMovieTimeOptionExact//使用精确的目前幀生成縮略圖
};
- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option 


//取消所有待處理的異步縮圖請求
- (void)cancelAllThumbnailImageRequests

//調用這個方法進行播放視訊的準備工作
- (void)prepareToPlay;
//擷取播放器的準備工作是否就緒
@property(nonatomic, readonly) BOOL isPreparedToPlay;
//調用此方法進行視訊的播放
- (void)play;
//調用此方法進行視訊播放的暫停操作
- (void)pause;
//調用此方法停止視訊播放
- (void)stop;
//目前視訊已播放的時間
@property(nonatomic) NSTimeInterval currentPlaybackTime;
//目前視訊的播放速度
@property(nonatomic) float currentPlaybackRate;
//調用此方法進行快進操作
- (void)beginSeekingForward;
//調用此方法進行快退操作
- (void)beginSeekingBackward;
//調用此方法結束快進或者快退操作
- (void)endSeeking;
           

播放器系統通知

//當縮放模式更改時發出
NSString * const MPMoviePlayerScalingModeDidChangeNotification

//在電影播放結束或使用者退出播放時發出
NSString * const MPMoviePlayerPlaybackDidFinishNotification
NSString * const MPMoviePlayerPlaybackDidFinishReasonUserInfoKey

//在播放狀态更改時釋出
NSString * const MPMoviePlayerPlaybackStateDidChangeNotification

//當網絡緩沖狀态改變時釋出
NSString * const MPMoviePlayerLoadStateDidChangeNotification

//目前播放的視訊改變時發送的通知
NSString * const MPMoviePlayerNowPlayingMovieDidChangeNotification

//将要進入全屏模式時發送的通知
NSString * const MPMoviePlayerWillEnterFullscreenNotification

//已經進入全屏時發送的通知
NSString * const MPMoviePlayerDidEnterFullscreenNotification

//将要退出全屏時發送的通知
NSString * const MPMoviePlayerWillExitFullscreenNotification

//已經退出全屏時發送的通知
NSString * const MPMoviePlayerDidExitFullscreenNotification


//當電影播放器開始或結束通過AirPlay播放視訊時釋出。
NSString * const MPMoviePlayerIsAirPlayVideoActiveDidChangeNotification

//當準備好顯示狀态改變時釋出。
NSString * const MPMoviePlayerReadyForDisplayDidChangeNotification
           

通知使用:

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];


-(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;
    }
}

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

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