iOS視頻功能模塊的開發(fā)

一、使用MPMoviePlayerController進(jìn)行視頻播放? ? ? ? MPMoviePlayerController是iOS中進(jìn)行視頻播放開發(fā)的一個(gè)控制類,里面涵蓋了視頻播放中大部分的需求功能,在使用這個(gè)框架時(shí),需要導(dǎo)入頭文件。

1、初始化方法

MPMoviePlayerController可以播放網(wǎng)絡(luò)視頻,也可以播放本地視頻,通過不同的URL來進(jìn)行初始化,例如本地視頻的初始化如下:

//視頻文件路徑

NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];

//視頻URL

NSURL *url = [NSURL fileURLWithPath:path];

//視頻播放對(duì)象

MPMoviePlayerController * movie = [[MPMoviePlayerController alloc] initWithContentURL:url];

初始化和完成相關(guān)配置后,我們需要將MPMoviePlayerController對(duì)象的View添加在我們需要的UI視圖上,這個(gè)控制器只提供的控制的相關(guān)功能,外部的UI并沒有為我們提供好。

2、相關(guān)屬性與方法

@property (nonatomic, copy) NSURL *contentURL;

視頻文件的url地址

@property (nonatomic, readonly) UIView *view;

播放器view,在使用之前,必須設(shè)置frame大小,然后將其添加在我們的UI視圖上

@property (nonatomic, readonly) UIView *backgroundView;

播放器背景顏色

@property (nonatomic, readonly) MPMoviePlaybackState playbackState;

播放器的當(dāng)前播放狀態(tài),枚舉定義如下:

typedef NS_ENUM(NSInteger, MPMoviePlaybackState) {

MPMoviePlaybackStateStopped,//停止播放

MPMoviePlaybackStatePlaying,//正在播放

MPMoviePlaybackStatePaused,//暫停播放

MPMoviePlaybackStateInterrupted,//中斷播放

MPMoviePlaybackStateSeekingForward,//快進(jìn)

MPMoviePlaybackStateSeekingBackward//快退

};

@property (nonatomic, readonly) MPMovieLoadState loadState;

播放器的網(wǎng)絡(luò)緩存狀態(tài),枚舉定義如下:

typedef NS_OPTIONS(NSUInteger, MPMovieLoadState) {

MPMovieLoadStateUnknown? ? ? ? = 0,//狀態(tài)未知

MPMovieLoadStatePlayable? ? ? = 1 << 0,//緩存數(shù)據(jù)足夠開始播放,但是視頻并沒有緩存完全

MPMovieLoadStatePlaythroughOK? = 1 << 1, //已經(jīng)緩存完成,如果設(shè)置了自動(dòng)播放,這時(shí)會(huì)自動(dòng)播放

MPMovieLoadStateStalled? ? ? ? = 1 << 2, //數(shù)據(jù)緩存已經(jīng)停止,播放將暫停

};

@property (nonatomic) MPMovieControlStyle controlStyle;

播放器風(fēng)格,枚舉如下:

typedef NS_ENUM(NSInteger, MPMovieControlStyle) {

MPMovieControlStyleNone,? ? ? // 無控制器

MPMovieControlStyleEmbedded,? // 嵌入視頻風(fēng)格

MPMovieControlStyleFullscreen, // 全屏播放風(fēng)格

MPMovieControlStyleDefault = MPMovieControlStyleEmbedded

};

@property (nonatomic) MPMovieRepeatMode repeatMode;

播放器的循環(huán)模式,枚舉如下:

typedef NS_ENUM(NSInteger, MPMovieRepeatMode) {

MPMovieRepeatModeNone,//播放結(jié)束后不循環(huán)

MPMovieRepeatModeOne//循環(huán)

};

@property (nonatomic) BOOL shouldAutoplay;

是否開啟自動(dòng)播放

@property (nonatomic, getter=isFullscreen) BOOL fullscreen;

設(shè)置是否充滿屏幕

- (void)setFullscreen:(BOOL)fullscreen animated:(BOOL)animated;

設(shè)置是否充滿屏幕,帶動(dòng)畫效果

@property (nonatomic) MPMovieScalingMode scalingMode;

設(shè)置播放器的填充方式,枚舉定義如下:

typedef NS_ENUM(NSInteger, MPMovieScalingMode) {

MPMovieScalingModeNone,? ? ? // 無縮放

MPMovieScalingModeAspectFit,? // 適應(yīng)大小模式

MPMovieScalingModeAspectFill, // 充滿可視范圍,可能會(huì)被裁剪

MPMovieScalingModeFill? ? ? ? // 縮放到充滿視圖

};

@property (nonatomic, readonly) BOOL readyForDisplay NS_AVAILABLE_IOS(6_0);

返回YES說明數(shù)據(jù)棧已經(jīng)緩存好數(shù)據(jù),返回NO則沒有緩存好

@property (nonatomic, readonly) MPMovieMediaTypeMask movieMediaTypes;

數(shù)據(jù)文件的格式,枚舉如下:

typedef NS_OPTIONS(NSUInteger, MPMovieMediaTypeMask) {

MPMovieMediaTypeMaskNone? = 0,//格式未知

MPMovieMediaTypeMaskVideo = 1 << 0,//音頻格式

MPMovieMediaTypeMaskAudio = 1 << 1//視頻格式

};

@property (nonatomic) MPMovieSourceType movieSourceType;

視頻的數(shù)據(jù)類型,枚舉如下:

typedef NS_ENUM(NSInteger, MPMovieSourceType) {

MPMovieSourceTypeUnknown,//類型未知

MPMovieSourceTypeFile,? ? // 文件類型

MPMovieSourceTypeStreaming // 數(shù)據(jù)流

};

@property (nonatomic, readonly) NSTimeInterval duration;

視頻文件的時(shí)長

@property (nonatomic, readonly) NSTimeInterval playableDuration;

緩存完成能夠播放的時(shí)長

@property (nonatomic, readonly) CGSize naturalSize;

視頻的原始大小

@property (nonatomic) NSTimeInterval initialPlaybackTime;

播放器開始播放的時(shí)間

@property (nonatomic) NSTimeInterval endPlaybackTime;

播放器結(jié)束播放的時(shí)間

@property (nonatomic) BOOL allowsAirPlay;

是否允許云端播放

- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)optio;

獲取視頻某一些時(shí)間點(diǎn)的縮略圖,參數(shù)枚舉如下,生成縮略圖的數(shù)據(jù)回調(diào)在后面的通知中詳說:

typedef NS_ENUM(NSInteger, MPMovieTimeOption) {

MPMovieTimeOptionNearestKeyFrame,//使用最近的關(guān)鍵幀生成縮略圖

MPMovieTimeOptionExact//使用精確的當(dāng)前幀生成縮略圖

};

3、系統(tǒng)相關(guān)通知

MPMoviePlayerController的系統(tǒng)回調(diào)并沒有采用代理的設(shè)計(jì)模式,而是采用的系統(tǒng)發(fā)通知,我們注冊(cè)觀察者,接收我們需要的通知。舉例幾種常用通知如下:

NSString * const MPMoviePlayerScalingModeDidChangeNotification;

播放器縮放產(chǎn)生改變時(shí)發(fā)送的通知

NSString * const MPMoviePlayerPlaybackDidFinishNotification;

播放結(jié)束時(shí)發(fā)送的通知

NSString * const MPMoviePlayerPlaybackStateDidChangeNotification;

播放狀態(tài)改變時(shí)發(fā)送的通知

NSString * const MPMoviePlayerLoadStateDidChangeNotification;

緩沖狀態(tài)改變時(shí)發(fā)送的通知

NSString * const MPMoviePlayerNowPlayingMovieDidChangeNotification;

當(dāng)前播放的視頻改變時(shí)發(fā)送的通知

NSString * const MPMoviePlayerWillEnterFullscreenNotification;

將要進(jìn)入全屏模式時(shí)發(fā)送的通知

NSString * const MPMoviePlayerDidEnterFullscreenNotification;

已經(jīng)進(jìn)入全屏?xí)r發(fā)送的通知

NSString * const MPMoviePlayerWillExitFullscreenNotification;

將要退出全屏?xí)r發(fā)送的通知

NSString * const MPMoviePlayerDidExitFullscreenNotification;

已經(jīng)退出全屏?xí)r發(fā)送的通知

NSString * const MPMoviePlayerThumbnailImageRequestDidFinishNotification;

獲取縮略圖完成時(shí)發(fā)送的通知

二、MPMoviePlayerViewController視頻視圖控制器

如果你很熟悉MVC,你可能會(huì)覺得MPMoviePlayerController的設(shè)計(jì)模式非常蹩腳,強(qiáng)行要求你將控制器的視圖分離出來加在另外的UI上,徒增的代碼邏輯的混亂,那么你想的沒錯(cuò),MPMoviePlayerViewController可能就是為了解決這個(gè)問題。

MPMoviePlayerViewController將視圖封裝在了一起,其中有一個(gè)成員對(duì)象是MPMoviePlayerController類型,類似C++中的has-a邏輯,我們只需要對(duì)MPMoviePlayerViewController進(jìn)行的簡(jiǎn)單的初始化后,對(duì)其中MPMoviePlayerController進(jìn)行其他配置,之后通過模態(tài)跳轉(zhuǎn)切換控制器即可。

方法如下:

- (instancetype)initWithContentURL:(NSURL *)contentURL;

初始化方法,和上面類似

@property (nonatomic, readonly) MPMoviePlayerController *moviePlayer;

播放器對(duì)象

- (void)presentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController *)moviePlayerViewController;

- (void)dismissMoviePlayerViewControllerAnimated;

viewController的模態(tài)跳轉(zhuǎn)方法,也可以通過導(dǎo)航push與pop

代碼示例如下:

@interface ViewController2 ()

@property(nonatomic,strong)MPMoviePlayerController * movie;

@property(nonatomic,strong)MPMoviePlayerViewController * viewController;

@end

@implementation ViewController2

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

[self playMovie:@"111"];

}

-(void)playMovie:(NSString *)fileName{

//視頻文件路徑

NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];

//視頻URL

NSURL *url = [NSURL fileURLWithPath:path];

//視頻播放對(duì)象

_viewController = [[MPMoviePlayerViewController alloc]initWithContentURL:url];

_movie=_viewController.moviePlayer;

// 注冊(cè)一個(gè)播放結(jié)束的通知

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(myMovieFinishedCallback:)

name:MPMoviePlayerPlaybackDidFinishNotification

object:_movie];

_movie.fullscreen=YES;

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[_movie play];

[self presentViewController:_viewController animated:YES completion:nil];

}

-(void)myMovieFinishedCallback:(NSNotification*)notify

{

//視頻播放對(duì)象

MPMoviePlayerController* theMovie = [notify object];

//銷毀播放通知

[[NSNotificationCenter defaultCenter] removeObserver:self

name:MPMoviePlayerPlaybackDidFinishNotification

object:theMovie];

[theMovie.view removeFromSuperview];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容