1.導(dǎo)入<MediaPlayer/MediaPlayer.h>頭文件
2.通過(guò)MPMoviePlayerViewController為我們提供的兩種方式:帶視圖和不帶視圖
__帶視圖(MPMoviePlayerViewController) : __系統(tǒng)已經(jīng)封裝好,可以拿來(lái)直接使用
創(chuàng)建控制器->modal展示
__不帶視圖(MPMoviePlayerController) : __ 可以滿足自定義的需求
MPMoviePlayerController和MPMoviePlayerViewController在iOS 9下目前已經(jīng)過(guò)期
帶視圖的示例代碼:
- (IBAction)clickStartPlayButton:(UIButton *)sender {
// 獲取視頻路徑 (這里使用了本地視頻文件,如果使用網(wǎng)絡(luò)視頻,設(shè)置網(wǎng)絡(luò)視頻URL即可)
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"minion_01.mp4" ofType:nil];
// 帶視圖
// 創(chuàng)建控制器
MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];
// 進(jìn)行modal展示
[self presentViewController:playerViewController animated:YES completion:nil];
}
在界面上添加了一個(gè)開(kāi)始播放按鈕,點(diǎn)擊時(shí)進(jìn)行視頻播放:
而且默認(rèn)的媒體控制系統(tǒng)已經(jīng)幫我們處理好,點(diǎn)擊Done會(huì)自動(dòng)dismiss
MPMoviePlayerViewController中包含一個(gè)moviePlayer屬性
@property (nonatomic, readonly) MPMoviePlayerController *moviePlayer;
MPMoviePlayerController繼承自NSObject,所以不能直接進(jìn)行present,通過(guò)<MPMediaPlayback>協(xié)議實(shí)現(xiàn)播放
不帶視圖的示例代碼:
聲明一個(gè)強(qiáng)引用類型的屬性:
方式一中帶有視圖的控制器是通過(guò)當(dāng)前控制器modal展現(xiàn),也就是Self強(qiáng)引用了MPMoviePlayerViewController,保證了MPMoviePlayerViewController不會(huì)被釋放
同理這里為了保證MPMoviePlayerController不被銷毀,所以聲明了一個(gè)強(qiáng)引用的屬性
@property(nonatomic,strong) MPMoviePlayerController *playerController;
按鈕點(diǎn)擊事件中:
- (IBAction)clickStartPlayButton:(UIButton *)sender {
// 不帶視圖 (可以自定義視圖)
// 獲取視頻路徑 (這里使用了本地視頻文件,如果使用網(wǎng)絡(luò)視頻,設(shè)置網(wǎng)絡(luò)視頻URL即可)
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"minion_01.mp4" ofType:nil];
// 創(chuàng)建播放器
self.playerController = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];
// 準(zhǔn)備播放
[self.playerController prepareToPlay];
// 開(kāi)始播放
[self.playerController play];
}
這樣雖然能夠?qū)崿F(xiàn)視頻播放,但是只能聽(tīng)到聲音,因?yàn)檫€未設(shè)置視圖
// 設(shè)置視圖
self.playerController.view.frame = [UIScreen mainScreen].bounds;
[self.view addSubview:self.playerController.view];
這樣就能顯示視圖了:
底部的媒體按鈕也是可以直接使用的,右下角按鈕點(diǎn)擊還能進(jìn)入全屏:
主要區(qū)別在于全屏模式上面會(huì)多出一個(gè)視圖:
進(jìn)入全屏后,媒體按鈕系統(tǒng)都已經(jīng)幫我們實(shí)現(xiàn)好了
Done按鈕點(diǎn)擊后,會(huì)退出全屏,并自動(dòng)暫停視頻
這里設(shè)置的是屏幕尺寸,如果需要設(shè)置窗口模式,手動(dòng)給視圖設(shè)置一個(gè)尺寸即可
self.playerController.view.frame = CGRectMake(100, 50, 200, 200);
上面提到了,當(dāng)自定義窗口視圖后,進(jìn)入全屏播放,點(diǎn)擊左上角的Done按鈕,會(huì)恢復(fù)窗口模式,暫停視圖,做進(jìn)一步處理,當(dāng)點(diǎn)擊Done按鈕時(shí),恢復(fù)窗口模式并銷毀視圖
實(shí)現(xiàn)方式:
1.監(jiān)聽(tīng)視頻控制器(從全屏恢復(fù)窗口)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stop) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
2監(jiān)聽(tīng)到狀態(tài)變化后進(jìn)一步判斷
全屏模式除了點(diǎn)擊Done按鈕會(huì)退出全屏,右上角也有一個(gè)退出全屏的按鈕
區(qū)別在于:點(diǎn)擊Done后會(huì)自動(dòng)停止播放,當(dāng)退出全屏并暫停播放時(shí)就是我們需要的狀態(tài),接下來(lái)就是移除自定義的播放視頻視圖
- (void)stop{
switch (self.playerController.playbackState) {
/*
MPMoviePlaybackStateStopped, 停止
MPMoviePlaybackStatePlaying, 正在播放
MPMoviePlaybackStatePaused, 暫停
MPMoviePlaybackStateInterrupted, 中斷
MPMoviePlaybackStateSeekingForward, 快進(jìn)
MPMoviePlaybackStateSeekingBackward 快退
*/
case MPMoviePlaybackStatePaused: //退出全屏&狀態(tài)為暫停時(shí),才是點(diǎn)擊Done按鈕
[self.playerController.view removeFromSuperview];
break;
default:
break;
}
}
完整實(shí)例代碼:
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h> //對(duì)<AVFoundation/AVFoundation.h>的封裝
@interface ViewController ()
@property(nonatomic,strong) MPMoviePlayerController *playerController;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 監(jiān)聽(tīng)窗口狀態(tài) 監(jiān)聽(tīng)通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stop) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
}
- (void)stop{
switch (self.playerController.playbackState) {
/*
MPMoviePlaybackStateStopped, 停止
MPMoviePlaybackStatePlaying, 正在播放
MPMoviePlaybackStatePaused, 暫停
MPMoviePlaybackStateInterrupted, 中斷
MPMoviePlaybackStateSeekingForward, 快進(jìn)
MPMoviePlaybackStateSeekingBackward 快退
*/
case MPMoviePlaybackStatePaused: //退出全屏&狀態(tài)為暫停時(shí),才是點(diǎn)擊Done按鈕
[self.playerController.view removeFromSuperview];
break;
default:
break;
}
}
// 開(kāi)始播放
- (IBAction)clickStartPlayButton:(UIButton *)sender {
// 不帶視圖 (可以自定義視圖)
// 獲取視頻路徑 (這里使用了本地視頻文件,如果使用網(wǎng)絡(luò)視頻,設(shè)置網(wǎng)絡(luò)視頻URL即可)
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"minion_01.mp4" ofType:nil];
// 創(chuàng)建播放器
self.playerController = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];
// 設(shè)置視圖
// self.playerController.view.frame = [UIScreen mainScreen].bounds;
self.playerController.view.frame = CGRectMake(100, 50, 200, 200);
[self.view addSubview:self.playerController.view];
// 準(zhǔn)備播放
[self.playerController prepareToPlay];
// 開(kāi)始播放
[self.playerController play];
}
@end
- MPMoviePlayerController中的關(guān)鍵屬性說(shuō)明:
@interface MPMoviePlayerController : NSObject <MPMediaPlayback>
// 視頻文件URL
@property (nonatomic, copy) NSURL *contentURL;
// 顯示視頻的視圖
@property (nonatomic, readonly) UIView *view;
// 播放視頻的背景視圖
@property (nonatomic, readonly) UIView *backgroundView;
// 播放狀態(tài)
@property (nonatomic, readonly) MPMoviePlaybackState playbackState;
// 加載狀態(tài)(加載是否成功)
@property (nonatomic, readonly) MPMovieLoadState loadState;
// 控制樣式(默認(rèn)顯示)
MPMovieControlStyleNone, // No controls (不顯示控制條)
MPMovieControlStyleEmbedded, // Controls for an embedded view(默認(rèn))
MPMovieControlStyleFullscreen, // Controls for fullscreen playback
@property (nonatomic) MPMovieControlStyle controlStyle;
// 重復(fù)
@property (nonatomic) MPMovieRepeatMode repeatMode;
// 是否自動(dòng)播放
@property (nonatomic) BOOL shouldAutoplay;
// 縮放 Defaults to MPMovieScalingModeAspectFit.
@property (nonatomic) MPMovieScalingMode scalingMode;
@end
@interface MPMoviePlayerController (MPMovieProperties)
// The types of media in the movie, or MPMovieMediaTypeNone if not known.
@property (nonatomic, readonly) MPMovieMediaTypeMask movieMediaTypes NS_DEPRECATED_IOS(2_0, 9_0, "Use AVPlayerViewController in AVKit.");
// The playback type of the movie. Defaults to MPMovieSourceTypeUnknown.
// Specifying a playback type before playing the movie can result in faster load times.
@property (nonatomic) MPMovieSourceType movieSourceType NS_DEPRECATED_IOS(2_0, 9_0, "Use AVPlayerViewController in AVKit.")
;
// The duration of the movie, or 0.0 if not known.
@property (nonatomic, readonly) NSTimeInterval duration NS_DEPRECATED_IOS(2_0, 9_0, "Use AVPlayerViewController in AVKit.")
;
// The currently playable duration of the movie, for progressively downloaded network content.
@property (nonatomic, readonly) NSTimeInterval playableDuration NS_DEPRECATED_IOS(2_0, 9_0, "Use AVPlayerViewController in AVKit.")
;
// The natural size of the movie, or CGSizeZero if not known/applicable.
@property (nonatomic, readonly) CGSize naturalSize NS_DEPRECATED_IOS(2_0, 9_0, "Use AVPlayerViewController in AVKit.")
;
// The start time of movie playback. Defaults to NaN, indicating the natural start time of the movie.
@property (nonatomic) NSTimeInterval initialPlaybackTime NS_DEPRECATED_IOS(2_0, 9_0, "Use AVPlayerViewController in AVKit.")
;
// The end time of movie playback. Defaults to NaN, which indicates natural end time of the movie.
@property (nonatomic) NSTimeInterval endPlaybackTime NS_DEPRECATED_IOS(2_0, 9_0, "Use AVPlayerViewController in AVKit.")
;
// Indicates whether the movie player allows AirPlay video playback. Defaults to YES on iOS 5.0 and later.
@property (nonatomic) BOOL allowsAirPlay NS_DEPRECATED_IOS(4_3, 9_0, "Use AVPlayerViewController in AVKit.")
;
//
@property (nonatomic, readonly, getter=isAirPlayVideoActive) BOOL airPlayVideoActive ;
@end
如果想要使用自定義的控制條樣式
可以設(shè)置controlStyle為MPMovieControlStyleNone,然后添加自定義的媒體控制視圖
需要注意的是:需要把事件添加到view視圖上, backgroundView下是不能監(jiān)聽(tīng)事件
在view屬性中已經(jīng)給了說(shuō)明
// The view in which the media and playback controls are displayed.
@property (nonatomic, readonly) UIView *view;
如果想要修改背景視圖,可以設(shè)置backgroundView
如果需要播放時(shí)默認(rèn)進(jìn)入全屏,在播放按鈕事件中,還可以重新設(shè)置播放視頻視圖的Frame為屏幕的bounds,并讓其旋轉(zhuǎn)90°,示例代碼:
[UIView animateWithDuration:0.1 animations:^{
// 播放視圖全屏橫向顯示
self.playerController.view.transform = CGAffineTransformRotate(self.playerController.view.transform, M_PI_2);
// 設(shè)置全屏
self.playerController.view.frame = [UIScreen mainScreen].bounds;
}];
MPMediaPlayback協(xié)議內(nèi)容:
@protocol MPMediaPlayback
// Prepares the current queue for playback, interrupting any active (non-mixible) audio sessions.
// Automatically invoked when -play is called if the player is not already prepared.
- (void)prepareToPlay;
// Returns YES if prepared for playback.
@property(nonatomic, readonly) BOOL isPreparedToPlay;
// Plays items from the current queue, resuming paused playback if possible.
- (void)play;
// Pauses playback if playing.
- (void)pause;
// Ends playback. Calling -play again will start from the beginnning of the queue.
- (void)stop;
// The current playback time of the now playing item in seconds.
@property(nonatomic) NSTimeInterval currentPlaybackTime;
// The current playback rate of the now playing item. Default is 1.0 (normal speed).
// Pausing will set the rate to 0.0. Setting the rate to non-zero implies playing.
@property(nonatomic) float currentPlaybackRate;
// The seeking rate will increase the longer scanning is active.
- (void)beginSeekingForward;
- (void)beginSeekingBackward;
- (void)endSeeking;
@end
// Posted when the prepared state changes of an object conforming to the MPMediaPlayback protocol changes.
// This supersedes MPMoviePlayerContentPreloadDidFinishNotification.
MP_EXTERN __TVOS_PROHIBITED NSString *const MPMediaPlaybackIsPreparedToPlayDidChangeNotification NS_DEPRECATED_IOS(3_2, 9_0);