9

我正在为 iPhone 编写一个应用程序,它将使用MPMoviePlayerViewController. 到目前为止,我已经让它播放电影,但是它在调试器中抛出了一些以CGContext. 我绞尽脑汁试图修复它。以下是我的代码的详细信息:

.h 文件:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MovieViewController : UIViewController {
    MPMoviePlayerViewController *playerController;
}

-(IBAction) playMovie:(id)sender;

.m 文件:

@interface MovieViewController ()
@end
@implementation

-(IBAction)playMovie:(id)sender {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"moviename" ofType:@"mp4"]];
playerController =  [[MPMoviePlayerViewController alloc]
                     initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playerController.moviePlayer play];
playerController = nil; 
}

当我运行程序时,电影将播放,但是当代码执行该行时:playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];出现以下错误:

<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextDrawShading: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0

我知道这invalid context 0x0意味着那些特定的变量没有值,但我不知道如何解决这个问题。任何帮助将不胜感激。

4

2 回答 2

24

I was having the same issue. Merely saying this:

MPMoviePlayerViewController* mpvc = 
    [[MPMoviePlayerViewController alloc] initWithContentURL: m];

was causing these invalid context error messages. The view controller's view (the fullscreen player) did appear when presented, and played the movie no problem; the only issue was these error messages, which I didn't want showing up in the console.

Taking a hint from Norman's solution, I simply wrapped the call in an artificial drawing context, like this:

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

It's silly, but it works.

于 2013-02-03T03:15:31.233 回答
8

我认为错误消息是 MPMoviePlayerViewController 中的错误

它们似乎并不致命,尽管我通过添加对 UIGraphicsBeginImageContext(self.view.frame.size); 的调用使它们消失了。在 readyPlayer() 和 viewDidLoad() 中。

我还在 dealloc() 中添加了 UIGraphicsEndImageContext() 来清理生成的上下文。

于 2012-11-18T20:50:32.787 回答