MPMoviePlayerViewController和MPMoviePlayerController
MPMoviePlayerViewController和MPMoviePlayerController 使用场合不一样
MPMoviePlayerViewController是在iOS3.2以后的平台上使用。MPMoviePlayerController在3.2之前使用,虽然在3.2之后也能使用,但是使用方法略有改变,建议3.2之后使用MPMoviePlayerViewController。
3.2之后,MPMoviePlayerController作为MPMoviePlayerViewController的一个属性存在。
iOS播放视频
iOS sdk为播放视频提供了非常简便方法,提供的MPMoviePlayerViewController类作为开发使用,在iOS4以前的版本是MPMoviePlayerController。
在iPhone开发规范中禁止使用私有API播放视频,因此播放画面的控制的控件都是有iPhone提供好的,我们没有别的选择。我们能做的:
加载URL中视频
播放、暂停视频
用户控制行为和缩放模式
产生通知
MPMoviePlayerController还可以播放互联网上的视频文件。但是建议你先将视频文件下载到本地,然后播放。如果你不这样做,iOS可能会拒绝播放很大的视频文件。
这个类定义在MediaPlayer framework中。在你的应用程序中,先添加这个引用,然后修改MediaPlayerViewController.h文件。
注意:要使用MPMoviePlayerViewController首先要加入MediaPlayer.framework.
ViewController.h
{
MPMoviePlayerViewController *moviePlayerView;
}
- (IBAction)playMovies:(id)sender;
-(void)playingDone;
@end
ViewController.m
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
//初始化通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(playingDone)name:MPMoviePlayerPlaybackDidFinishNotificationobject:nil];
}
- (IBAction)playMovies:(id)sender {
//初始化播放器并且设定播放模式
moviePlayerView = [[MPMoviePlayerViewControlleralloc]initWithContentURL:[NSURLfileURLWithPath: [[NSBundlemainBundle]pathForResource:@"po"ofType:@"mp4"]]];
//设定播放模式
moviePlayerView.moviePlayer.controlStyle. =MPMovieControlStyleFullscreen;
//控制模式(触摸)
moviePlayerView.moviePlayer.scalingMode =MPMovieScalingModeAspectFill;
// MPMovieControlStyleNone
//MPMovieControlStyleEmbedded
//MPMovieControlStyleDefault
//开始播放
[[[UIApplicationsharedApplication]keyWindow]addSubview:moviePlayerView.view];
/*视频文件可以播放资源目录、沙箱目录和网络播放。本例中我们采用资源目录。
moviePlayerView.moviePlayer属性是MPMoviePlayerController类型,它有的controlStyle属性
可以控制播放行为,它的取值有:
MPMovieControlStyleFullscreen
MPMovieControlStyleNone没有播放控件
MPMovieControlStyleEmbedded
MPMovieControlStyleDefault
MPMoviePlayerController类还有scalingMode属性用于控制影片的尺寸,它的取值有:
MPMovieScalingModeNone原始尺寸
MPMovieScalingModeAspectFit缩放到一个填充方向
MPMovieScalingModeAspectFill填充两边可能会切除一部分
MPMovieScalingModeFill填充两边可能会改变比例
*/}
-(void)playingDone
{
NSLog(@"播放完成");
[moviePlayerView.viewremoveFromSuperview];
moviePlayerView = nil;
}
-(void)dealloc
{
//移除通知
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
@end
以下是使用MPMoviePlayerViewController播放视频的代码:
- (void) playVideo:(NSURL *) movieURL//根据视频url播放视频{
playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
playerViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:playerViewController animated:YES];
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
}
////
- (void) playVideoFinished:(NSNotification *)theNotification//当点击Done按键或者播放完毕时调用此函数
{
MPMoviePlayerController *player = [theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player stop];
[playerViewController dismissModalViewControllerAnimated:YES];
}
var commentManager = new blogCommentManager();commentManager.renderCommen