ios播放視頻文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController。前者是一個view,後者是個Controller。區別就是 MPMoviePlayerViewController裡面包含了一個MPMoviePlayerController
注意:MPMoviePlayerViewController 必須 presentMoviePlayerViewControllerAnimated方式添加,否則Done按鈕是不會響應通知MPMoviePlayerPlaybackDidFinishNotification事件的;
首先要包含 #import
MPMovieControlModeDefault 顯示播放 / 暫停、音量和時間控制
MPMovieControlModeVolumeOnly 只顯示音量控制
MPMovieControlModeHidden 沒有控制器
你可以使用下列寬高比值:
MPMovieScallingModeNone 不做任何縮放
MPMovieScallingModeAspectFit 適應屏幕大小,保持寬高比
MPMovieScallingModeAspectFill 適應屏幕大小,保持寬高比,可裁剪
MPMovieScallingModeFill 充滿屏幕,不保持寬高比
//通知
MPMoviePlayerContentPreloadDidFinishNotification 當電影播放器結束對內容的預加載後發出。因為內容可以在僅加載了一部分的情況下播放,所以這個通知可能在已經播放後才發出。
MPMoviePlayerScallingModeDidChangedNotification 當用戶改變了電影的縮放模式後發出。用戶可以點觸縮放圖標,在全屏播放和窗口播放之間切換。
MPMoviePlayerPlaybackDidFinishNotification 當電影播放完畢或者用戶按下了 Done 按鈕後發出
===============================================================================
需要引進的框架:MediaPlayer.framework
第一步:引進框架設置屬性
#import RootViewController.h #import@interface RootViewController () @property (nonatomic, strong) MPMoviePlayerController *moviePlayer; @end @implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 5.調用播放器
//播放網絡視頻
NSString *urlString = @http://video.szzhangchu.com/qiaokeliruanxinbudingA.mp4;
//播放本地視圖,找到文件的路徑
// NSString *urlStr = [[NSBundle mainBundle] pathForResource:@優酷網-唐豆豆微信搖一搖慘被騙.mp4 ofType:nil];
[self createMPPlayerController:urlString];
}
- (void)createMPPlayerController:(NSString *)string
{
// 1.初始化播放器
//准備網址
// NSURL *urlString = [NSURL fileURLWithPath:fileNamePath];
NSURL *urlString = [NSURL URLWithString:string];
//初始化播放器
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
//准備播放
// [_moviePlayer prepareToPlay];
//設置moviePlayer的frame
_moviePlayer.view.frame = self.view.frame;
//添加到父視圖
[self.view addSubview:_moviePlayer.view];
// 2.配置屬性
//是否自動播放,默認是NO
_moviePlayer.shouldAutoplay = YES;
//設置播放器的樣式
[_moviePlayer setControlStyle:(MPMovieControlStyleFullscreen)];
//開始播放
[_moviePlayer play];
// 3.注冊通知
//注冊通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinshed:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
// 4.實現通知中的方法
- (void)movieFinshed:(NSNotification *)sender
{
//取出通知中心的moviePlayer
MPMoviePlayerController *movie = [sender object];
//移除觀察者
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
//將movie移出父視圖
[movie.view removeFromSuperview];
}
