MPMoviePlayerController无法在iOS 5 xcode(带故事板)中工作,但在iOS 4中运行良好 [英] MPMoviePlayerController not working in iOS 5 xcode (with storyboard), but works perfectly in iOS 4

查看:62
本文介绍了MPMoviePlayerController无法在iOS 5 xcode(带故事板)中工作,但在iOS 4中运行良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用iOS 5 beta附带的新xcode播放电影时遇到问题。我创建了一个简单的项目(带有故事板和所有),并将此代码添加到按钮:

I'm having problems playing a movie with the new xcode that comes with iOS 5 beta. I created a simple project (with storyboards and all), and added this code to a button:

MPMoviePlayerController *moviePlayer;
NSString *path = [[NSBundle mainBundle] pathForResource:@"position" ofType:@"m4v"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
//[moviePlayer setControlStyle:MPMovieControlStyleDefault];
[moviePlayer.view setFrame: self.view.bounds];  // player's frame must match parent's
[self.view addSubview: moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];

令人困惑的是,当我将代码放入使用以前版本构建的项目时,它可以正常工作xcode(特别是iOS 4.3附带的那个)。

The confusing thing is that it works perfectly when I throw the code into a project built with a previous version of xcode (specifically the one that comes with iOS 4.3).

任何想法?

推荐答案

Objective-C的新手,但我会试一试。使用Xcode 4.2(提供iOS5代码),默认情况下,新项目启用了ARC(自动引用计数)。对于你给出的代码,因为你在这段代码中声明了moviePlayer,所以当moviePlayer走出块时会自动释放。在较旧的项目中,moviePlayer会继续存在,可能会造成内存泄漏。我通过在类的头文件中声明moviePlayer来启用默认的Xcode 4.2 ARC设置,这意味着只有在释放该类的对象实例时才会释放它。

New to Objective-C but I'll give a shot at it. With Xcode 4.2 (which offers to code for iOS5), by default, new projects have ARC (Automatic Reference Counting) turned on. For the code you gave, since you declare the moviePlayer inside this block of code, moviePlayer automatically gets released when it steps out of the block. In older projects, moviePlayer would just linger on, potentially creating a memory leak. I got it working in with the default Xcode 4.2 ARC setting turned on by declaring moviePlayer in the header file of the class, which means it gets released only when the object instance of that class is released.

这篇关于MPMoviePlayerController无法在iOS 5 xcode(带故事板)中工作,但在iOS 4中运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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