iOS播放视频
iOS sdk为播放视频提供了非常简便方法,提供的M PMoviePlayerViewController类 作为开发使用,在iOS4以前的版本是MPMoviePlayerController。
在iPhone开发规范中禁止使用私有API播放视频,因此播放画面的控制的控件都是有iPhone提供好的,我们没有别的选择。我们能做的:
加载URL中视频
播放、暂停视频
用户控制行为和缩放模式
产生通知
MPMoviePlayerController还可以播放互联网上的视频文件。但是建议你先将视频文件下载到本地,然后播放。如果你不这样做,iOS可能会拒绝播放很大的视频文件。
这个类定义在 MediaPlayer framework 中。在你的应用程序中,先添加这个引用,然后修改 MediaPlayerViewController.h 文件。
ViewController.h#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@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];
//初始化通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(playingDone) name:MPMoviePlayerPlaybackDidFinishNotificationobject:nil];
}
- (IBAction)playMovies:(id)sender {
//初始化播放器并且设定播放模式
moviePlayerView = [[ MPMoviePlayerViewController alloc]initWithContentURL:[NSURLfileURLWithPath: [[NSBundlemainBundle]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
原文:http://blog.csdn.net/eduora_meimei/article/details/23095699
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!