MPMoviePlayerController不播放任何视频内容(由于ARC和内存管理) [英] MPMoviePlayerController not playing any video content [due to ARC and memory management]

查看:103
本文介绍了MPMoviePlayerController不播放任何视频内容(由于ARC和内存管理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MPMoviePlayerController在用户按下按钮时将电影视图追加到现有视图,从而在iOS applocation中播放视频. 该视图在所有位置均显示为黑框,但没有任何反应. 我希望这部电影可以开始播放,但是什么也没发生,而且应用程序也没有提示为什么.

I am trying to get a video playing in an iOS applocation, using MPMoviePlayerController to append a movie-view to an existing view when a user pushes a button. The view appears as a black box on the right position and everything, but nothing happens. I am expecting the movie to start playing, but nothing happens and no hints from the application on why.

任何人都可以看到哪里出错了吗?

Can anyone see where this goes wrong?

-(IBAction) playButtonClicked:(id)sender
{
    NSString* path = [[NSBundle mainBundle] pathForResource:@"sample_mpeg4" ofType:@"mp4"];
    NSLog(@"Using videoPath %@", path);
    NSURL* url = [NSURL fileURLWithPath:path];
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.view addSubview:player.view];
    player.view.frame = CGRectMake(10, 10, 300, 220);
   [player play];

}

该电影存在并且记录了正确的路径,但是仍然没有电影播放.

The movie exists and logs the correct path, but still no movie playing.

2012-07-06 11:51:13.492 experiments[84702:12203] Using videoPath /Users/marius/Library/Application Support/iPhone Simulator/5.1/Applications/9799C851-8D2B-4CFE-8CFA-A4C4C954787F/experiments.app/sample_mpeg4.mp4

据我所阅读和理解,这应该是有效的. 有什么建议或建议吗?

From what I have read and understood this should be working. Any hints or suggestions on what to try?

我将影片移到了HTTP服务器(fg.mp4),并且正在添加访问日志. 单击播放按钮时,会出现通常的黑色窗口. 在服务器端,我在日志中找到两个新条目:

I moved the movie to a HTTP-server (fg.mp4) and are tailing the access-logs. When clicking the play-button the usual black window appears. Serverside i find two new entries in the log:

168.122.x.x - - [06/Jul/2012:22:42:33 +0200] "GET /fg.mp4 HTTP/1.1" 304 192 "-" "AppleCoreMedia/1.0.0.9B176 (iPhone Simulator; U; CPU OS 5_1 like Mac OS X; en_us)"
168.122.x.x - - [06/Jul/2012:22:42:33 +0200] "GET /fg.mp4 HTTP/1.1" 206 33304 "-" "AppleCoreMedia/1.0.0.9B176 (iPhone Simulator; U; CPU OS 5_1 like Mac OS X; en_us)"

因此,手机将获取电影内容的一部分,并且-显然-准备在需要时获取其余内容. 我缺少的MPMoviePlayerController是否有一些明显的参数?

So the phone gets parts of the movie content, and -apparently- prepares to get the rest when needed. Is there some obvious parameter for the MPMoviePlayerController I'm missing?

更新:解决方案和有效代码

该问题是由ARC引起的,并且在调用设置控制器的方法后没有保留对MPMoviePlayerController的引用. 解决方案:在类中添加一个类变量,并使用该变量在电影的生命周期内保留对控制器的引用.

The problem is caused by ARC and the fact that no reference to the MPMoviePlayerController were kept after calling the method setting up the controller. The solution: Adding a class-variable to the class and use this to keep a reference to the controller for the lifetime of the movie.

在类声明的早期(任何消息/函数定义之外):

So early in the class declaration (outside any message/function-definitions):

MPMoviePlayerController* mpController;

前面提到的playButtonClicked使用此变量来跟踪控制器:

And the abeformentioned playButtonClicked uses this variable to keep track of the controller:

NSString* path = [[NSBundle mainBundle] pathForResource:@"sample_mpeg4" ofType:@"mp4"];
NSURL* url = [NSURL fileURLWithPath:path];
mpController = [[MPMoviePlayerController alloc] initWithContentURL:url];
mpController.view.frame = CGRectMake(10, 10, 300, 220);
[self.view addSubview:mpController.view];
[mpController play];

推荐答案

与我对

As with my answer to MPMoviePlayerController playback terminates before video finishes, this looks to be a memory management (instance lifetime) issue. Assuming you are compiling with ARC, the player variable is released when your playButtonClicked: method returns. At that point the MPMoviePlayerController instance takes its bat and ball, and goes home.

尝试将MPMoviePlayerController实例分配给强实例变量或属性.

Try assigning the MPMoviePlayerController instance to a strong instance variable or property.

这篇关于MPMoviePlayerController不播放任何视频内容(由于ARC和内存管理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆