Search code examples
objective-cmpmovieplayercontrollerios8.3

play video with MPMoviePlayerController


I'm trying to make a video start up alone when I opened my view however just the sound of it works. The view is blank. I'm using MPMoviePlayerController to do this.

my code is this:

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self playVideo];
}

-(void)playVideo {
    NSString *videoFile = [[NSBundle mainBundle] pathForResource:@"fds" ofType:@"mp4"];
    self.moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:videoFile]];
    [self.playerView addSubview:self.moviePlayer.view];
    self.moviePlayer.fullscreen = YES;
    self.moviePlayer.allowsAirPlay = YES;
    [self.moviePlayer play];
}

Solution

  • Try moving your playVideo call to viewWillAppear