kidd风的IOS日志之播放音乐(MPMoviePlayerViewController和MPMoviePlayerController)

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

@interface ViewController :UIViewController

{

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 allowComments=true,isLogined=false,cb_blogId=118219,cb_entryId=2639763,cb_blogApp=currentBlogApp,cb_blogUserGuid='40c25404-0da3-e111-aa3f-842b2b196315',cb_entryCreatedDate='2012/8/15 11:32:00';loadViewCount(cb_entryId);

var commentManager = new blogCommentManager();commentManager.renderCommen




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值