Search code examples
iphoneiosobjective-cipadipod

Video doesn't show up on screen


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.


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];