您的位置:首页 > 产品设计 > 产品经理

iOS用MPMoviePlayerViewController 播放MP4视频

2014-07-10 23:10 477 查看
本文转自--红黑联盟

iOS播放视频

iOS sdk为播放视频提供了非常简便方法,提供的MPMoviePlayerViewController类作为开发使用,在iOS4以前的版本是MPMoviePlayerController。

在iPhone开发规范中禁止使用私有API播放视频,因此播放画面的控制的控件都是有iPhone提供好的,我们没有别的选择。我们能做的:

加载URL中视频

播放、暂停视频

用户控制行为和缩放模式

产生通知

MPMoviePlayerController还可以播放互联网上的视频文件。但是建议你先将视频文件下载到本地,然后播放。如果你不这样做,iOS可能会拒绝播放很大的视频文件。

这个类定义在MediaPlayer framework中。在你的应用程序中,先添加这个引用,然后修改MediaPlayerViewController.h文件。
ViewController.h

#import

#import

@interface ViewController : UIViewController

{

MPMoviePlayerViewController *moviePlayerView;

}

- (IBAction)playMovies:(id)sender;

-(void)playingDone;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

//初始化通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playingDone) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}

- (IBAction)playMovies:(id)sender {

//初始化播放器并且设定播放模式

moviePlayerView = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]pathForResource:@"po" ofType:@"mp4"]]];

//设定播放模式

moviePlayerView.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;

//控制模式(触摸)

moviePlayerView.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;

// MPMovieControlStyleNone

//MPMovieControlStyleEmbedded

//MPMovieControlStyleDefault

//开始播放

[[[UIApplication sharedApplication]keyWindow]addSubview:moviePlayerView.view];

/*视频文件可以播放资源目录、沙箱目录和网络播放。本例中我们采用资源目录。

moviePlayerView.moviePlayer属性是MPMoviePlayerController类型,它有的controlStyle属性

可以控制播放行为,它的取值有:

MPMovieControlStyleFullscreen

MPMovieControlStyleNone没有播放控件

MPMovieControlStyleEmbedded

MPMovieControlStyleDefault

MPMoviePlayerController类还有scalingMode属性用于控制影片的尺寸,它的取值有:

MPMovieScalingModeNone原始尺寸

MPMovieScalingModeAspectFit缩放到一个填充方向

MPMovieScalingModeAspectFill填充两边可能会切除一部分

MPMovieScalingModeFill填充两边可能会改变比例

*/}

-(void)playingDone

{

NSLog(@"播放完成");

[moviePlayerView.view removeFromSuperview];

moviePlayerView = nil;

}

-(void)dealloc

{

//移除通知

[[NSNotificationCenter defaultCenter]removeObserver:self];

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: