0

我是一名新开发人员,因此我仍在学习并且知道我做错了一些事情。我有一个分段控制对象,当用户按下其中一个分段时,它会播放视频。我将它设置到他们按下片段的位置,然后必须按下播放按钮才能播放视频。我想剪掉播放按钮并让它自动播放。这就是我遇到麻烦的地方。我找到了 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];
}
4

2 回答 2

2

不要播放视频,只需准备播放并将其放入shouldAutoPlayas 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
}
于 2013-04-27T06:21:30.087 回答
2

你试过[theMovie play];吗?

更新:我刚刚注意到您设置shouldAutoplaytheMovie,但是您提供的是 的另一个实例MPMoviePlayerViewController,即moviePlayer. 这就是您的电影没有自动播放的原因。你应该有:

[self presentMoviePlayerViewControllerAnimated:theMovie];
于 2012-09-02T09:05:06.243 回答