使用MPMoviePlayer播放电影 - 控件将不会显示

时间:2011-06-08 16:57:03

标签: objective-c cocoa-touch ios mpmovieplayercontroller

我想播放带控件的电影 - 播放,停止,快进等。我使用此代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *movieUrl = [NSURL fileURLWithPath:
                       [[NSBundle mainBundle] pathForResource:@"Spot" 
                                                       ofType:@"mp4"]];

    MPMoviePlayerController* myMovie=[[MPMoviePlayerController alloc] 
                                      initWithContentURL:movieUrl];

    //disable scaling of our movie
    myMovie.scalingMode = MPMovieScalingModeNone;
    [myMovie.view setFrame: myView.bounds];  
    [myView addSubview: myMovie.view];

    [[myMovie view] setFrame:[myView bounds]];
    myMovie.initialPlaybackTime = 2.0;

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(movieFinished:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:myMovie]; 
    myMovie.scalingMode = MPMovieScalingModeAspectFill;
    myMovie.controlStyle = MPMovieControlStyleEmbedded;
    [myMovie play];
    // Do any additional setup after loading the view from its nib.
}

电影有效,但没有控件。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试这是否有效......

-(void)viewWillAppear:(BOOL)animated
{
    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"Spot.mp4" ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.view addSubview:moviePlayer.view];
    moviePlayer.view.frame = CGRectMake(0, 0, 300, 400);  
    [moviePlayer play];
}

您将获得所有控件。