MPMoviePlayerController

视频播放在iPhone中是再重要不过了,今天要在30行内解决iPhone视频播放的问题!

3, 设置MPtest1AppDelegate初始化并添加MyMPViewController的view到window

@interface MPtest1AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyMPViewController *mpViewController;
}
@end
- (void)applicationDidFinishLaunching:(UIApplication *)application {
mpViewController = [[MyMPViewController alloc] init];
[window addSubview:mpViewController.view];
[window makeKeyAndVisible];
}
4, 设置MyMPViewController在一个按钮点击后播放视频

@interface MyMPViewController : UIViewController {
UIButton *playButton;
}
@end
@interface MyMPViewController()
- (void)playVideo;
@end
@implementation MyMPViewController
- (id)init
{
if (self = [super init]) {
playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
//在这里初始化那个按键
playButton.frame = CGRectMake(100, 100, 100, 30);
[self.view addSubview:playButton];
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];

[playButton setTitle:@"播放" forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playVideo) forControlEvents:UIControlEventTouchUpInside];
//设置按键属性,然后添加点击后触发的方法函数
}

- (void)playVideo
{
MPMoviePlayerController *moviePlayer;
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"]]];
//初始化视频播放器对象,并传入被播放文件的地址
moviePlayer.movieControlMode = MPMovieControlModeDefault;
[moviePlayer play];
//此处有内存溢出,简单程序就先算了...
}

- (void)dealloc {
[playButton release];
[super dealloc];
}
@end
5, 编译运行

————————————————

整个程序非常简单,排除掉没有用的,必须用的,真正MP播放的部分也不到10行。

简单iPhone视频播放器(1)补



标签:MPMoviePlayerController, 视频播放器
分类目录: 16 - 多媒体(音视频), iPhone开发, 源代码 | 评论
视频播放与通告(Notification)
2009年07月13日, 12:41 下午
N 以下是一个非常简单的视频播放代码,并且可以简单的理解Cocoa中的通告使用方法。

- (void)playMovieAtURL:(NSURL*)theURL //简单的方法,传入一个视频地址
{
MPMoviePlayerController *thePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:theURL]; //初始化播放器

thePlayer.scalingMode = MPMovieScalingModeAspectFill; //下面有详细的缩放解释
//thePlayer.userCanShowTransportConstrols = NO; 这个是苹果文档上的原始代码,是错的...
thePlayer.movieControlMode = MPMovieControlModeDefault; //缺省视频控制

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification thePlayer];
//注册本地类为thePlayer的通告对象,通告方法为“视频播放结束”,
//回应方法为myMovieFinishedCallback
//也就是说,当视频播放结束以后,运行该方法

[thePlayer play]; //播放!
}
- (void)myMovieFinishedCallback:(NSNotification*)aNotification
{
MPMoviePlayerController *thePlayer = [aNotification object];
//从通告中导入这个播放器对象,如果播放器是单独的类成员,那就不用这步咯。

[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
thePlayer];
//这步非常非常重要,一定要将被监听对象卸载,
//否则本地对象卸载后,监听对象为nil,软件会崩溃的

[thePlayer release]; //释放视频对象
}
scalingMode缩放模式的四种参数:
-MPMovieScalingModeNone
-不对视频进行缩放

-MPMovieScalingModeAspectFit
-视频缩放到内框,4比3的视频会在左右留下黑框

-MPMovieScalingModeAspectFill
-视频缩放到外框,4比3的视频上下会被削掉一部分

-MPMovieScalingModeFill
-视频被拉伸,失去原始比例

原文更详细,请查阅:[url]http://c.gzl.name/archives/tag/mpmovieplayercontroller[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值