Question

I'm building an application containing a XML feed with a link to a movie, which I would like to play whenever the user clicks on an image.

For that purpose I'm using the MPMoviePlayerViewController. Testing the app on the simulator has given me the expected results so far, however, when I test the app on an iPhone, the player plays correctly but with no sound.

I've looked on the internet where a few articles told me that the app's performance differs a lot from the performance the simulator gives you. Therefore I made a "leak" test with Instruments, which told me that whenever I started playing a movie, some bytes got dropped or leaked. Could that have something to do with the sudden drop of sound? If so, how would I solve it?

It's my first experience of testing apps on iPhone, so I got pretty shocked seeing how badly the app performed. Here's the code for the movieplayer-part.

#pragma mark Video Controls
-(void)playVideo:(id)sender{
    moviePlaying=TRUE;
    MPMoviePlayerViewController *playerViewController;
    playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[data objectForKey:@"[att]url"]]];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:[playerViewController moviePlayer]];
    MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] init] autorelease];
    player = [playerViewController moviePlayer];

    [self.view addSubview:playerViewController.view];
    player.controlStyle = MPMovieControlStyleDefault;
    player.shouldAutoplay = YES;
    [player setFullscreen:YES animated:YES];
} 

- (void)moviePlayBackDidExitFullscreen:(NSNotification*)notification{
    MPMoviePlayerViewController *moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];

    [moviePlayer.view removeFromSuperview];
    [moviePlayer release];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerViewController *moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];

    moviePlaying=FALSE;
    [moviePlayer.view removeFromSuperview];
    [moviePlayer release];
}
#pragma mark VideoControls End

I hope you can light up the problem.

Thanks in advance,

/Brinck10

Was it helpful?

Solution

You've got some very serious confusion here. You should not be creating both an MPMoviePlayerViewController and an MPMoviePlayerController. Use one or the other. And these lines make no sense:

MPMoviePlayerController *player = 
    [[[MPMoviePlayerController alloc] init] autorelease];
player = [playerViewController moviePlayer];

You are creating a MPMoviePlayerController and assigning it to player but then throwing it away, replacing it your existing MPMoviePlayerViewController's moviePlayer.

Let's drop back ten yards and punt. The easy way to play a movie / stream sound / whatever is to use MPMoviePlayerViewController. It is a view controller, so just shove it where you would use a UIViewController. Simplest is to show it modally:

NSURL* m = // whatever;
MPMoviePlayerViewController* mpvc = 
    [[MPMoviePlayerViewController alloc] initWithContentURL: m];
[self presentModalViewController:mpvc animated:YES];
[mpvc release];

This creates the MPMoviePlayerController and its view and presents the whole shebang for you in fullscreen mode only. Simple and easy. The modal view is even dismissed for you when the user taps Done.

Using MPMoviePlayerController is more involved. It is NOT a UIViewController. But it has a view. Your job, if you want the user to see anything, is to grab that view, give it a frame, and stick it into your interface:

NSURL* m = // whatever;
MPMoviePlayerController* mp = 
    [[MPMoviePlayerController alloc] initWithContentURL:m];
self.mpc = mp; // property with retain policy
[mp release];
self.mpc.view.frame = CGRectMake(10, 10, 300, 230);
[self.view addSubview:self.mpc.view];

This makes the movie view part of your interface. If the movie is a real movie, it leaves the user free to toggle into fullscreen mode or not. If the movie is just a sound, the user doesn't have a way to do that; there's just a stop/go button and a slider.

Either way there is an MPMoviePlayerController somewhere in the story so you can register for its notifications, tell it to play/stop, etc. But don't confuse or meld the two approaches. That's probably the cause of all your issues here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top