大家好,我按照网络上的帖子写一个用MPMoviePlayerViewController 播放本地与远程视频的例子,碰到不少问题望大虾能帮忙指导:
我是在一个UIViewControl模板中添加两个按钮:一个点击后执行播放本地代码,另一个执行远程视频代码。
1. 播放本地视频:能播放mp4,但不能播放3gp格式(对应的MP4和3gp资源文件我肯定已经加到resource里):现象是执行play后,后面系统马上回调我注册的finishCallback。
2. 播放远程视频:也不能成功,系统马上回调我注册的finishCallback。我的代码如下(刚开始学习IOS,大虾快帮帮忙啊,:)):
-(IBAction)playUrlVideo //按钮点击后执行此处远程播放代码
{
NSString *url =@"http://www.itc.virginia.edu/network/videotest/when-lg.mov";
isUrl = YES;
[self playerRun:url];
}-(IBAction)playFileVideo //按钮点击后执行此处本地播放代码
{
NSString *url = [[NSBundle mainBundle] pathForResource:@"cv" ofType:@"mp4"];
isUrl = NO;
[self playerRun:url];
}-(void)playerRun:(NSString *)url
{
NSURL *urlPath;
if (isUrl) {
urlPath = [[NSURL alloc] initWithString:url];
}
else {
urlPath = [[NSURL alloc] initFileURLWithPath:url];
}
MPMoviePlayerViewController *pc = [[MPMoviePlayerViewController alloc]initWithContentURL:urlPath];
self.player = pc;
[pc release];
//add view.
[self.view addSubview:player.view];
//set animation.
[self presentMoviePlayerViewControllerAnimated:player]; //set config and start playing.
player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
player.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[player.moviePlayer play];
//callback
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[urlPath release];
}-(void)playCallback:(NSNotification *)notification
{
NSError *error = [[notification userInfo] objectForKey:@"error"];
if (error) {
//这里没有进来
NSLog(@"DidFinishNotification has error %@",error);
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; if (player) {
[self dismissMoviePlayerViewControllerAnimated];
[player.moviePlayer stop];
player.moviePlayer.initialPlaybackTime=-1.0;
[player release];
player = nil;
}
}
我是在一个UIViewControl模板中添加两个按钮:一个点击后执行播放本地代码,另一个执行远程视频代码。
1. 播放本地视频:能播放mp4,但不能播放3gp格式(对应的MP4和3gp资源文件我肯定已经加到resource里):现象是执行play后,后面系统马上回调我注册的finishCallback。
2. 播放远程视频:也不能成功,系统马上回调我注册的finishCallback。我的代码如下(刚开始学习IOS,大虾快帮帮忙啊,:)):
-(IBAction)playUrlVideo //按钮点击后执行此处远程播放代码
{
NSString *url =@"http://www.itc.virginia.edu/network/videotest/when-lg.mov";
isUrl = YES;
[self playerRun:url];
}-(IBAction)playFileVideo //按钮点击后执行此处本地播放代码
{
NSString *url = [[NSBundle mainBundle] pathForResource:@"cv" ofType:@"mp4"];
isUrl = NO;
[self playerRun:url];
}-(void)playerRun:(NSString *)url
{
NSURL *urlPath;
if (isUrl) {
urlPath = [[NSURL alloc] initWithString:url];
}
else {
urlPath = [[NSURL alloc] initFileURLWithPath:url];
}
MPMoviePlayerViewController *pc = [[MPMoviePlayerViewController alloc]initWithContentURL:urlPath];
self.player = pc;
[pc release];
//add view.
[self.view addSubview:player.view];
//set animation.
[self presentMoviePlayerViewControllerAnimated:player]; //set config and start playing.
player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
player.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[player.moviePlayer play];
//callback
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[urlPath release];
}-(void)playCallback:(NSNotification *)notification
{
NSError *error = [[notification userInfo] objectForKey:@"error"];
if (error) {
//这里没有进来
NSLog(@"DidFinishNotification has error %@",error);
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; if (player) {
[self dismissMoviePlayerViewControllerAnimated];
[player.moviePlayer stop];
player.moviePlayer.initialPlaybackTime=-1.0;
[player release];
player = nil;
}
}
解决方案 »
- XCode 4.5 在IOS5.1.1真机调试秒退
- xcode 屏幕触摸事件
- xcode project navigator里面的路径错了怎么办?
- iOS GIS地图开发教程——UCMap for iOS 开发教程.pdf
- IPHONE 误升级
- ISO 中admob 的添加问题
- cocos2d-x 开发iOS游戏,会用到C语言编写socket程序么?
- 主线程等待block执行结果后执行
- iOS 中使用FMDB 查询最大值 一直提示I could not find the column named 'id'
- 怎么给图片命名呢
- 新手关于xcode4.2
- 各位高手 教教我安装iphone的开发环境吧
http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/Reference/Reference.html看文档吧
MPEG-4 视频最高可达 2.5 Mbps,640 x 480,每秒 30 帧,Simple Profile 采用 AAC-LC 声音格式,每声道最高 160 Kbps,48 KHz,立体声为 .m4v、.mp4 与 .mov 文件格式;
Motion JPEG (M-JPEG) 最高可达 35 Mbps,1280 x 720,每秒 30 帧,音频为 ulaw 格式,PCM 立体声为 .avi 文件格式木有3gp呀⋯⋯