Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone MPMoviePlayerViewController CGContext Errors

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.

like image 759
Joseph Avatar asked Nov 02 '12 21:11

Joseph


1 Answers

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.

like image 190
matt Avatar answered Sep 24 '22 08:09

matt