应该在Movie PlayerController上自动播放

时间:2011-10-09 14:33:40

标签: ios mpmovieplayercontroller autoplay

我是一名新开发人员,因此我仍在学习并知道我做错了什么。我有一个分段控制对象,当用户按下其中一个段时,它会播放视频。我将它设置为按下片段的位置,然后必须按播放按钮才能播放视频。我想剪掉播放按钮并让它自动播放。这是我遇到麻烦的地方。我找到了shouldAutoplay选项但是当我使用它并剪切按钮时,根本不会将我带到视频中。我确定我没有正确使用shouldAutoplay选项。希望得到一些帮助或至少在正确的方向上有一点。

- (IBAction)playMovie:(id)sender;

{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"mytestimony" ofType:@"m4v"];
    NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    theMovie.shouldAutoplay = YES;
    theMovie.scalingMode = MPMovieScalingModeAspectFill;
    [theMovie autorelease];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];
}

2 个答案:

答案 0 :(得分:2)

您是否尝试过[theMovie play];

更新:我刚刚注意到您在shouldAutoplay中设置了theMovie,但您提供的是MPMoviePlayerViewController的另一个实例,即moviePlayer 。这就是为什么你的电影没有自动播放的原因。你应该改为:

[self presentMoviePlayerViewControllerAnimated:theMovie];

答案 1 :(得分:2)

请勿播放视频,只需准备播放并将shouldAutoPlay设置为NO,因为您不希望视频播放器自动播放。

-(void) SetVideoFile:(NSString *)fileName {

    videoFileName=fileName;
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:fileName]];

    moviePlayer.initialPlaybackTime = 0;
    moviePlayer.view.frame = self.view.frame ;
    [orientationHandler VideoStart];
    [self.view addSubview:moviePlayer.view] ;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayer];
    moviePlayer.shouldAutoplay = NO;
    [moviePlayer prepareToPlay];
}

-(void)moviePlaybackDidFinish:(NSNotification *)notification {

    // your custom code which you want to play after the player did finish playing
}