xcode iOS 4.3 内联 MPMoviePlayerController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5442412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:39:48  来源:igfitidea点击:

iOS 4.3 Inline MPMoviePlayerController

iphoneobjective-cxcode

提问by Umair A.

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.

我在 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]; 
}

采纳答案by Andy S.

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

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

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 MoviePlayersample project) and just adjust AppDelegate's initAndPlayMoviethis way:

好的,对于示例代码,您可以使用名为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];
    }
}

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 localand click on Play Movie.

这个很粗糙,因为它没有设置框架或使视图居中,但是当您转到本地并单击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.

我看到的唯一缺点是全屏不会变黑。也就是说,示例项目非常奇怪,而且编写得不是很好。也就是说它显示非全屏视频。