0

I imported the MPMoviePlayerController in my VideosView.h. In my VideosView.m i embed the following code:

    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mp4" inDirectory:@"images"];
    NSLog(@"%@", path2);

    MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
    myPlayer.shouldAutoplay = YES;
    myPlayer.repeatMode = MPMovieRepeatModeOne;
    myPlayer.fullscreen = YES;
    myPlayer.movieSourceType = MPMovieSourceTypeFile;
    myPlayer.scalingMode = MPMovieScalingModeAspectFit;
    myPlayer.contentURL =[NSURL fileURLWithPath:path2];
    myPlayer.view.frame = CGRectMake(0, 0, 500, 500);
    myPlayer.scalingMode = MPMovieScalingModeFill;
    myPlayer.controlStyle = MPMovieControlModeDefault;

    [self addSubview:myPlayer.view];
    [myPlayer play];

I've found this example on Stackoverflow, but can't get it working. The link to my video is correct (yes it's in the images folder). I get a 500 by 500px black rectangle on my screen (the frame ofcourse) but no video is playing.

Some help would be great. W.

4

2 回答 2

2

全局定义 myPlayer 对象

在您的代码中, myPlayer 的生命周期以变量的范围结束。如果您在方法内部创建了。播放器以该方法的范围结束。

@property(nonatomic, strong) MPMoviePlayerController *myPlayer;

然后从你想要的任何地方初始化,

_myPlayer = [[MPMoviePlayerController alloc] init];
于 2013-04-30T19:22:43.037 回答
1
    self.moviePlayerView = [[MPMoviePlayerViewController alloc]initWithContentURL:videoURL];
    movie = [self.moviePlayerView moviePlayer];

    movie.controlStyle = MPMovieControlStyleNone;

    [movie setControlStyle:MPMovieControlStyleFullscreen];

    self.moviePlayerView.moviePlayer.shouldAutoplay=YES;

    [movie prepareToPlay];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateChanged)
                                                                                  name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];


    self.moviePlayerView.view.frame = CGRectMake(0.0f, 0.0f, 304.0f, 221.0f);

    [[self.moviePlayerView moviePlayer]play];


add bewlow..

 - (void) playbackStateChanged
 {
self.moviePlayerView.moviePlayer.shouldAutoplay=YES;

  MPMoviePlaybackState playbackState = [self.moviePlayerView.moviePlayer playbackState];

  switch (playbackState)
  {

    case MPMoviePlaybackStateStopped :

        break;

    case MPMoviePlaybackStatePlaying :


        break;

    case MPMoviePlaybackStateInterrupted :


        break;

    }

 }
于 2013-05-01T05:59:17.357 回答