I am trying to get a video playing an iOS application using MPMoviePlayerController to add a movie view to an existing view when the user clicks a button. The view looks like a black box in the correct position and that's it, but nothing happens. I expect the movie to start playing, but nothing happens and no tips from the app about why.
Can anyone see where this is happening?
-(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 records the correct path, but the movie does not play.
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 read and understood, this should work. Any tips or suggestions on what to try?
I moved the movie to an HTTP server (fg.mp4) and closed the access logs. When you click on the play button, a regular black window appears. Serverside I find two entries in the log:
168.122.xx - - [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.xx - - [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)"
Thus, the phone receives parts of the contents of the film, and, accordingly, prepares to get peace when necessary. Is there some obvious parameter for MPMoviePlayerController that I am missing?
UPDATE: Solution and working code
The problem is caused by ARC and the fact that after calling the method that configures the controller, the link to MPMoviePlayerController
not saved. Solution: add a class variable to the class and use it to store a reference to the controller for the duration of the movie.
At the beginning of a class declaration (outside of any messages / definition functions):
MPMoviePlayerController* mpController;
And the abbreviation used playButtonClicked
uses this variable to track 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];