iOS 4.3 Inline MPMoviePlayerController

时间:2011-03-26 12:51:21

标签: iphone objective-c xcode

我在我的UIView中使用MPMoviePlayerController,目标是将其作为内联视图放在View上。问题是除了全屏之外代码不起作用。

-(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]; 
}

1 个答案:

答案 0 :(得分:1)

Apple的示例代码存在缺陷,或者说过时了。您需要将 moviePlayer的视图添加为视图的子视图。像这样:

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];
...

这应该可以解决问题。

抱歉,我没有看到你已经添加了子视图。

好的,对于示例代码,您可以使用名为 MoviePlayer_iPhone 的XCode示例项目(在MPMoviePlayerController的XCode文档中,您将找到 MoviePlayer 示例项目的链接并以这种方式调整 AppDelegate的initAndPlayMovie

-(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];
    }
}

这是粗略的,因为它没有设置框架或视图居中,但是当你去本地并点击播放电影时它应该显示电影。< / p>

我看到的唯一缺点是全屏不会变黑。那说样本项目非常奇怪,编写得不是很好。这表示它显示非全屏视频。