Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPMoviePlayerController not playing any video content [due to ARC and memory management]

I am trying to get a video playing in an iOS applocation, using MPMoviePlayerController to append a movie-view to an existing view when a user pushes a button. The view appears as a black box on the right position and everything, but nothing happens. I am expecting the movie to start playing, but nothing happens and no hints from the application on why.

Can anyone see where this goes wrong?

-(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 logs the correct path, but still no movie playing.

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 have read and understood this should be working. Any hints or suggestions on what to try?


I moved the movie to a HTTP-server (fg.mp4) and are tailing the access-logs. When clicking the play-button the usual black window appears. Serverside i find two new entries in the log:

168.122.x.x - - [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.x.x - - [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)"

So the phone gets parts of the movie content, and -apparently- prepares to get the rest when needed. Is there some obvious parameter for the MPMoviePlayerController I'm missing?


UPDATE: Solution and working code

The problem is caused by ARC and the fact that no reference to the MPMoviePlayerController were kept after calling the method setting up the controller. The solution: Adding a class-variable to the class and use this to keep a reference to the controller for the lifetime of the movie.

So early in the class declaration (outside any message/function-definitions):

MPMoviePlayerController* mpController;

And the abeformentioned playButtonClicked uses this variable to keep track of 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];
like image 375
mariusnn Avatar asked Jul 06 '12 16:07

mariusnn


1 Answers

As with my answer to MPMoviePlayerController playback terminates before video finishes, this looks to be a memory management (instance lifetime) issue. Assuming you are compiling with ARC, the player variable is released when your playButtonClicked: method returns. At that point the MPMoviePlayerController instance takes its bat and ball, and goes home.

Try assigning the MPMoviePlayerController instance to a strong instance variable or property.

like image 157
Martin Kenny Avatar answered Sep 30 '22 20:09

Martin Kenny