开发者

iOS 4.3 Inline MPMoviePlayerController

I am using MPMoviePlayerController in my UIView and the goal is to 开发者_高级运维put it there on View as inline view. The problem is that the code doesn't work except the full screen.

-(IBAction)startVideo {
    //start video here
    NSURL *path = [[NSURL alloc] initWithString:[self localVideoPath:NO]];

    // Create custom movie player   
    MPMoviePlayerController *moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:path] autorelease];

    [moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
    [moviePlayer setControlStyle:MPMovieControlStyleNone];
    [moviePlayer setFullscreen:FALSE];

    // May help to reduce latency
    [moviePlayer prepareToPlay];

    [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(onMSAASDone:)
        name:MPMoviePlayerPlaybackDidFinishNotification
        object:moviePlayer];


    //---play partial screen---
    //moviePlayer.view.frame = CGRectMake(0, 0, 200, 300);
    moviePlayer.view.frame = image.frame;
    //[[moviePlayer view] setFrame: [image bounds]];

    [image removeFromSuperview];

    [self.view addSubview:moviePlayer.view];

    // Show the movie player as modal
    //[self presentModalViewController:moviePlayer animated:YES];

    // Prep and play the movie
    [moviePlayer play]; 
}


The sample code from Apple is flawed or let's say outdated. You need to add the moviePlayer's view as subview to your view. Something like this:

MPMoviePlayerController *moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:path] autorelease];
...
// Adjust positioning where I used the bound of the outer view (of type UIView)
moviePlayer.view.frame = outerView.bounds;
// Now add the movie player to the outer view
[outerView addSubView:moviePlayer.view];
...

This should do the trick.

Sorry, I did not see that you added the subview already.

Okay, for a sample code you can take the XCode sample project called MoviePlayer_iPhone (inside the XCode documentation for MPMoviePlayerController you'll find a link for the MoviePlayer sample project) and just adjust AppDelegate's initAndPlayMovie this way:

-(void)initAndPlayMovie:(NSURL *)movieURL
{
    // Initialize a movie player object with the specified URL
    MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    if (mp)
    {
        // save the movie player object
        self.moviePlayer = mp;
        [mp release];

        // Apply the user specified settings to the movie player object
        [self setMoviePlayerUserSettings];

        self.moviePlayer.view.frame = self.window.bounds;            
        [self.window addSubview:self.moviePlayer.view];

        // Play the movie!
        [self.moviePlayer play];
    }
}

This one is crude because it does not set the frame or centers the view but it should display the movie when you go to local and click on Play Movie.

The only drawback I saw was that the fullscreen is not going black. That said the sample project is pretty weird and not very well written. That said it displays the non-fullscreen video.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜