Search code examples
iosmpmovieplayercontroller

iOS - I Can't reproduce a video with MPMoviePlayerController


I'm trying to reproduce a video with MPMoviePlayerController by this way:

    var url:NSURL = NSURL(fileURLWithPath: "/var/mobile/Containers/Data/Application/D64F96C8-B00F-41D8-AC38-DB07121D00FA/Documents/video_comp.mov", isDirectory: false)

    if (NSFileManager.defaultManager().fileExistsAtPath(url.path!)) {
        println("FILE EXISTS") //File exists
    } else {
        println("FILE DOESN'T EXIST")
    }

    var moviePlayer = MPMoviePlayerController(contentURL: url)
    moviePlayer.view.frame = CGRectMake(0, 0, 200, 200)
    self.view.addSubview(moviePlayer.view)

    moviePlayer.scalingMode = MPMovieScalingMode.AspectFit
    moviePlayer.shouldAutoplay = true
    moviePlayer.prepareToPlay()

    if moviePlayer.isPreparedToPlay {
        println("PREPARED")
    } else {
        println("NOT PREPARED") //.isPreparedToPlay is always false
    }

    moviePlayer.play()

But it's never reproduced. First of all I checked if the file exists and it exists. Then I checked if moviePlayer isPrepraredToPlay but it seems is not prepared before playing. Do you have any idea why I can't reproduce it?

Thanks


Solution

  • this is working for me:

     myPlayer = [[MPMoviePlayerController alloc] init];
    [myPlayer setScalingMode:MPMovieScalingModeAspectFit];
    // [myPlayer setControlStyle:MPMovieControlStyleFullscreen];
    [myPlayer setContentURL:[NSURL fileURLWithPath:filePath]];
    myPlayer.movieSourceType = MPMovieControlStyleEmbedded;
    //myPlayer.fullscreen = YES;
    myPlayer.view.frame = CGRectMake(0, 0, 200, 200);   //self.view.bounds;
    [myPlayer prepareToPlay];
    if(myPlayer.isPreparedToPlay){
        NSLog(@"preparada para verse!");
    } else {
        NSLog(@"No preparada para verse!");
    }
    [self.view addSubview:myPlayer.view];
    [myPlayer play];
    

    In my app I use fullscreen video but I've put in comments the fullscreen code and I've changed self.view.bounds for your size: CGRectMake(0,0,200,200). The funny thing is that I also get the not prepared message but the video works perfect! I hope this help!