1.導入<MediaPlayer/MediaPlayer.h>頭文件
2.通過MPMoviePlayerViewController為我們提供的兩種方式:帶視圖和不帶視圖
__帶視圖(MPMoviePlayerViewController) : __系統已經封裝好,可以拿來直接使用
創建控制器->modal展示
__不帶視圖(MPMoviePlayerController) : __ 可以滿足自定義的需求
MPMoviePlayerController和MPMoviePlayerViewController在iOS 9下目前已經過期
帶視圖的示例代碼:
- (IBAction)clickStartPlayButton:(UIButton *)sender {
// 獲取視頻路徑 (這里使用了本地視頻文件,如果使用網絡視頻,設置網絡視頻URL即可)
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"minion_01.mp4" ofType:nil];
// 帶視圖
// 創建控制器
MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];
// 進行modal展示
[self presentViewController:playerViewController animated:YES completion:nil];
}
在界面上添加了一個開始播放按鈕,點擊時進行視頻播放:
而且默認的媒體控制系統已經幫我們處理好,點擊Done會自動dismiss
MPMoviePlayerViewController中包含一個moviePlayer屬性
@property (nonatomic, readonly) MPMoviePlayerController *moviePlayer;
MPMoviePlayerController繼承自NSObject,所以不能直接進行present,通過<MPMediaPlayback>協議實現播放
不帶視圖的示例代碼:
聲明一個強引用類型的屬性:
方式一中帶有視圖的控制器是通過當前控制器modal展現,也就是Self強引用了MPMoviePlayerViewController,保證了MPMoviePlayerViewController不會被釋放
同理這里為了保證MPMoviePlayerController不被銷毀,所以聲明了一個強引用的屬性
@property(nonatomic,strong) MPMoviePlayerController *playerController;
按鈕點擊事件中:
- (IBAction)clickStartPlayButton:(UIButton *)sender {
// 不帶視圖 (可以自定義視圖)
// 獲取視頻路徑 (這里使用了本地視頻文件,如果使用網絡視頻,設置網絡視頻URL即可)
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"minion_01.mp4" ofType:nil];
// 創建播放器
self.playerController = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];
// 準備播放
[self.playerController prepareToPlay];
// 開始播放
[self.playerController play];
}
這樣雖然能夠實現視頻播放,但是只能聽到聲音,因為還未設置視圖
// 設置視圖
self.playerController.view.frame = [UIScreen mainScreen].bounds;
[self.view addSubview:self.playerController.view];
這樣就能顯示視圖了:
底部的媒體按鈕也是可以直接使用的,右下角按鈕點擊還能進入全屏:
主要區別在于全屏模式上面會多出一個視圖:
進入全屏后,媒體按鈕系統都已經幫我們實現好了
Done按鈕點擊后,會退出全屏,并自動暫停視頻
這里設置的是屏幕尺寸,如果需要設置窗口模式,手動給視圖設置一個尺寸即可
self.playerController.view.frame = CGRectMake(100, 50, 200, 200);
上面提到了,當自定義窗口視圖后,進入全屏播放,點擊左上角的Done按鈕,會恢復窗口模式,暫停視圖,做進一步處理,當點擊Done按鈕時,恢復窗口模式并銷毀視圖
實現方式:
1.監聽視頻控制器(從全屏恢復窗口)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stop) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
2監聽到狀態變化后進一步判斷
全屏模式除了點擊Done按鈕會退出全屏,右上角也有一個退出全屏的按鈕
區別在于:點擊Done后會自動停止播放,當退出全屏并暫停播放時就是我們需要的狀態,接下來就是移除自定義的播放視頻視圖
- (void)stop{
switch (self.playerController.playbackState) {
/*
MPMoviePlaybackStateStopped, 停止
MPMoviePlaybackStatePlaying, 正在播放
MPMoviePlaybackStatePaused, 暫停
MPMoviePlaybackStateInterrupted, 中斷
MPMoviePlaybackStateSeekingForward, 快進
MPMoviePlaybackStateSeekingBackward 快退
*/
case MPMoviePlaybackStatePaused: //退出全屏&狀態為暫停時,才是點擊Done按鈕
[self.playerController.view removeFromSuperview];
break;
default:
break;
}
}
完整實例代碼:
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h> //對<AVFoundation/AVFoundation.h>的封裝
@interface ViewController ()
@property(nonatomic,strong) MPMoviePlayerController *playerController;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 監聽窗口狀態 監聽通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stop) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
}
- (void)stop{
switch (self.playerController.playbackState) {
/*
MPMoviePlaybackStateStopped, 停止
MPMoviePlaybackStatePlaying, 正在播放
MPMoviePlaybackStatePaused, 暫停
MPMoviePlaybackStateInterrupted, 中斷
MPMoviePlaybackStateSeekingForward, 快進
MPMoviePlaybackStateSeekingBackward 快退
*/
case MPMoviePlaybackStatePaused: //退出全屏&狀態為暫停時,才是點擊Done按鈕
[self.playerController.view removeFromSuperview];
break;
default:
break;
}
}
// 開始播放
- (IBAction)clickStartPlayButton:(UIButton *)sender {
// 不帶視圖 (可以自定義視圖)
// 獲取視頻路徑 (這里使用了本地視頻文件,如果使用網絡視頻,設置網絡視頻URL即可)
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"minion_01.mp4" ofType:nil];
// 創建播放器
self.playerController = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];
// 設置視圖
// self.playerController.view.frame = [UIScreen mainScreen].bounds;
self.playerController.view.frame = CGRectMake(100, 50, 200, 200);
[self.view addSubview:self.playerController.view];
// 準備播放
[self.playerController prepareToPlay];
// 開始播放
[self.playerController play];
}
@end
- MPMoviePlayerController中的關鍵屬性說明:
@interface MPMoviePlayerController : NSObject <MPMediaPlayback>
// 視頻文件URL
@property (nonatomic, copy) NSURL *contentURL;
// 顯示視頻的視圖
@property (nonatomic, readonly) UIView *view;
// 播放視頻的背景視圖
@property (nonatomic, readonly) UIView *backgroundView;
// 播放狀態
@property (nonatomic, readonly) MPMoviePlaybackState playbackState;
// 加載狀態(加載是否成功)
@property (nonatomic, readonly) MPMovieLoadState loadState;
// 控制樣式(默認顯示)
MPMovieControlStyleNone, // No controls (不顯示控制條)
MPMovieControlStyleEmbedded, // Controls for an embedded view(默認)
MPMovieControlStyleFullscreen, // Controls for fullscreen playback
@property (nonatomic) MPMovieControlStyle controlStyle;
// 重復
@property (nonatomic) MPMovieRepeatMode repeatMode;
// 是否自動播放
@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
如果想要使用自定義的控制條樣式
可以設置controlStyle為MPMovieControlStyleNone,然后添加自定義的媒體控制視圖
需要注意的是:需要把事件添加到view視圖上, backgroundView下是不能監聽事件
在view屬性中已經給了說明
// The view in which the media and playback controls are displayed.
@property (nonatomic, readonly) UIView *view;
如果想要修改背景視圖,可以設置backgroundView
如果需要播放時默認進入全屏,在播放按鈕事件中,還可以重新設置播放視頻視圖的Frame為屏幕的bounds,并讓其旋轉90°,示例代碼:
[UIView animateWithDuration:0.1 animations:^{
// 播放視圖全屏橫向顯示
self.playerController.view.transform = CGAffineTransformRotate(self.playerController.view.transform, M_PI_2);
// 設置全屏
self.playerController.view.frame = [UIScreen mainScreen].bounds;
}];
MPMediaPlayback協議內容:
@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);