首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MPMoviePlayerController不能正常工作

MPMoviePlayerController不能正常工作
EN

Stack Overflow用户
提问于 2013-04-12 17:44:29
回答 2查看 1.1K关注 0票数 0

我正在制作一个正在尝试播放视频的应用程序。视频正常启动,但视频屏幕在4秒后变为黑色。我不知道问题出在哪里。

另外,当我设置player.movieplayer.shouldautoplay = NO时,此行没有任何效果,视频会自动开始。

以下是代码:

代码语言:javascript
运行
复制
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];

NSURL *urlObj = [NSURL fileURLWithPath:urlString];

UIGraphicsBeginImageContext(CGSizeMake(1,1));
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:urlObj];
UIGraphicsEndImageContext();

[player.view setBounds:self.view.bounds];
// when playing from server source type shoud be MPMovieSourceTypeStreaming
[player.moviePlayer setMovieSourceType:MPMovieSourceTypeStreaming];
[player.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];

player.moviePlayer.shouldAutoplay =  NO;

[self.view addSubview:player.view];
[player.moviePlayer play];

我是不是漏掉了什么?

我试图获取视频的总时长(使用mpmovieplayercontroller的duration属性),但显示为0.0。如何获取视频时长??

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-12 17:50:16

代码语言:javascript
运行
复制
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
NSURL *urlObj = [NSURL fileURLWithPath:urlString];

UIGraphicsBeginImageContext(CGSizeMake(1,1));
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:urlObj];
UIGraphicsEndImageContext();

[player.view setBounds:self.view.bounds];
// when playing from server source type shoud be MPMovieSourceTypeStreaming
[player.moviePlayer setMovieSourceType:MPMovieSourceTypeStreaming]; // I was missing this line therefore video was not playing
[player.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];


[self.view addSubview:player.view];
[player.moviePlayer play];
票数 4
EN

Stack Overflow用户

发布于 2013-04-14 21:10:07

这里有几个问题:

  1. 对于这种用法(将播放器集成到视图中),您应该使用MPMoviePlayerController,而不是MPMoviePlayerViewController。如果您希望拥有一个可以使用presentMoviePlayerViewControllerAnimated:.

呈现的自包含视图控制器,请使用MPMoviePlayerViewController

  1. 假设你使用的是ARC,主要的问题是没有任何东西保持对你的播放器对象的引用。因此,播放器在您创建它后不久就会消失。您应该通过将其分配给视图控制器的属性或实例变量来保留对它的引用。

有关这方面的完整示例,请参阅Till的优秀answer to a similar question.

  1. 我不确定UIGraphicsBeginImageContextUIGraphicsEndImageContext调用的目的是什么,但我看不出这里需要它们。

至于shouldAutoplay = NO,视频仍然在开始,因为您随后立即调用了play

播放器的duration属性仅在接收到MPMovieDurationAvailableNotification后才包含有用的值。您需要执行类似以下内容的操作才能访问实际持续时间:

代码语言:javascript
运行
复制
__weak MediaPlayerController *weakSelf = self;
[[NSNotificationCenter defaultCenter] addObserverForName:MPMovieDurationAvailableNotification object:self.player queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
  NSLog(@"Movie duration: %lf", weakSelf.player.duration);
}];

完成后,使用removeObserver:name:object:删除观察者。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15968020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档