9

I am writing an app for the iPhone that will play some movies utilizing MPMoviePlayerViewController. So far I have gotten it to play movies, however it is throwing some errors in the debugger that all start with CGContext. I have wracked my brain in trying to fix it. Here are the details of my code:

.h file:

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

-(IBAction) playMovie:(id)sender;

.m file:

@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; 
}

When I run the program the movie will play, however when the code executes the line: playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; the following errors occur:

<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

I do know that invalid context 0x0 means that those specific variables do not have a value, but I have no idea how to remedy this. Any help would be greatly appreciated.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Joseph
  • 145
  • 1
  • 9

2 Answers2

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 2
    this works. though can you please explain why there's problem at the first place and why this solution works. In my ipad 6.1 simulator, the problem won't occur; it only occurs in my iphone 6.1 simulator. – sbs Feb 21 '13 at 02:32
  • thanks! really saved me I was starting to go insane with this issue. – Mahdi Yusuf Jun 09 '13 at 00:10
  • I also had to disable my all-purpose breakpoint for "all exceptions", as this issue would cause the debugger to breakpoint-stop 30+ times. With that breakpoint disabled, and the graphicsContext fix above, I get no problems from the simulator/debugger. – ObjectiveTC Sep 20 '13 at 23:36
  • @ObjectiveTC I agree. The problem is that "all exceptions" can stop on exceptions even if they are caught by the framework - they are not really problems, and would not crash your app in the wild, but they are acting as breakpoints when running in Xcode. - However, that has nothing to do with the above problem; it really *is* a problem. It's not an exception; it's a bunch of error messages appearing in the log. – matt Sep 21 '13 at 15:13
  • @Till Pointless. It's fixed in iOS 7, and they're not likely to go back and fix iOS 6. – matt Nov 05 '13 at 00:24
  • Glad to hear that it got fixed. I haven't done any iOS 7 development (but plenty of 3,4,5 and 6). – Till Nov 05 '13 at 00:48
8

I think the error messages are a bug in MPMoviePlayerViewController

They don't seem to be fatal, although I've made them go away by adding a call to UIGraphicsBeginImageContext(self.view.frame.size); in both readyPlayer() and viewDidLoad().

I also added a UIGraphicsEndImageContext() to dealloc() to clean up the generated contexts.

Norman
  • 81
  • 1