在iOS中播放視頻可以使用MediaPlayer.framework種的MPMoviePlayerController類來(lái)完成,它支持本地視頻和網(wǎng)絡(luò)視頻播放益愈。這個(gè)類實(shí)現(xiàn)了MPMediaPlayback協(xié)議,因此具備一般的播放器控制功能夷家,例如播放蒸其、暫停、停止等库快。
但是MPMediaPlayerController自身并不是一個(gè)完整的視圖控制器摸袁,如果要在UI中展示視頻需要將view屬性添加到界面中。下面列出了MPMoviePlayerController的常用屬性和方法:
注意MPMediaPlayerController
的狀態(tài)等信息并不是通過(guò)代理來(lái)和外界交互的义屏,而是通過(guò)通知中心靠汁,因此從上面的列表中可以看到常用的一些通知。
由于MPMoviePlayerController
本身對(duì)于媒體播放做了深度的封裝闽铐,使用起來(lái)就相當(dāng)簡(jiǎn)單:
創(chuàng)建MPMoviePlayerController
對(duì)象蝶怔,設(shè)置frame
屬性,將MPMoviePlayerController
的view
添加到控制器視圖中兄墅。下面的示例中將創(chuàng)建一個(gè)播放控制器并添加播放狀態(tài)改變及播放完成的通知:
//
// ViewController.m
// MPMoviePlayerController
//
// Created by Kenshin Cui on 14/03/30.
// Copyright (c) 2014年 cmjstudio. All rights reserved.
//
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
@interface ViewController ()
@property (nonatomic,strong) MPMoviePlayerController *moviePlayer;//視頻播放控制器
@end
@implementation ViewController
#pragma mark - 控制器視圖方法
- (void)viewDidLoad {
[super viewDidLoad];
//播放
[self.moviePlayer play];
//添加通知
[self addNotification];
}
-(void)dealloc{
//移除所有通知監(jiān)控
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - 私有方法
/**
* 取得本地文件路徑
*
* @return 文件路徑
*/
-(NSURL *)getFileUrl{
NSString *urlStr=[[NSBundle mainBundle] pathForResource:@"movie.mp4" ofType:nil];
NSURL *url=[NSURL fileURLWithPath:urlStr];
return url;
}
/**
* 取得網(wǎng)絡(luò)文件路徑
*
* @return 文件路徑
*/
-(NSURL *)getNetworkUrl{
NSString *urlStr= @"http://www.realank.com/movie.mp4";//@"http://mediaserver.enorth.com.cn:8083/videos/live/57/33/d3Hr2eCssqyV1/d3Hr2eCssqyV1.M3U8";
urlStr=[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:urlStr];
return url;
}
/**
* 創(chuàng)建媒體播放控制器
*
* @return 媒體播放控制器
*/
-(MPMoviePlayerController *)moviePlayer{
if (!_moviePlayer) {
NSURL *url=[self getFileUrl];
_moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:url];
_moviePlayer.repeatMode = MPMovieRepeatModeOne;
_moviePlayer.view.frame=CGRectMake(20, 100, 300, 300);
_moviePlayer.view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
_moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
[self.view addSubview:_moviePlayer.view];
}
return _moviePlayer;
}
/**
* 添加通知監(jiān)控媒體播放控制器狀態(tài)
*/
-(void)addNotification{
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];
[notificationCenter addObserver:self selector:@selector(movieLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:self.moviePlayer];
}
/**
* 播放狀態(tài)改變踢星,注意播放完成時(shí)的狀態(tài)是暫停
*
* @param notification 通知對(duì)象
*/
-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
switch (self.moviePlayer.playbackState) {
case MPMoviePlaybackStatePlaying:
NSLog(@"正在播放...");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暫停播放.");
break;
case MPMoviePlaybackStateStopped:
NSLog(@"停止播放.");
break;
default:
NSLog(@"播放狀態(tài):%li",self.moviePlayer.playbackState);
break;
}
}
/**
* 播放完成
*
* @param notification 通知對(duì)象
*/
-(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
NSLog(@"播放完成.%li",self.moviePlayer.playbackState);
}
-(void)movieLoadStateChanged:(NSNotification *)notification{
if (self.moviePlayer.loadState == MPMovieLoadStateUnknown) {
self.moviePlayer.contentURL = [self getFileUrl];
[self.moviePlayer play];
}
}
@end
因?yàn)橐曨l播放器全屏以后,會(huì)有快進(jìn)快退的按鈕隙咸,但是按了按鈕以后沐悦,會(huì)造成黑屏成洗,所以增加了name:MPMoviePlayerLoadStateDidChangeNotification通知,并且當(dāng)
loadState為MPMovieLoadStateUnknown的時(shí)候藏否,代碼強(qiáng)制重新播放視頻
當(dāng)全屏了視頻播放器以后泌枪,如果想支持橫屏播放,需要再appDelegate里添加對(duì)應(yīng)的方法:
//在didFinishLaunchingWithOptions中調(diào)用這個(gè)方法
- (void)addMoviePlaybackRotateNotification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
}
- (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
self.allowRotation = YES;
}
- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
self.allowRotation = NO;
}
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}
return UIInterfaceOrientationMaskPortrait;
}
詳情查看stack overflow中的內(nèi)容