Question

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.

Was it helpful?

Solution

Define myPlayer object globally

in your code, the life of the myPlayer ends with the scope of the variable. If you have created inside a method. The player ends with the scope of that method.

@property(nonatomic, strong) MPMoviePlayerController *myPlayer;

then init from anywhere you want,

_myPlayer = [[MPMoviePlayerController alloc] init];

OTHER TIPS

    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;

    }

 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top