iOS - 我无法使用MPMoviePlayerController重现视频

时间:2014-11-05 15:37:36

标签: ios mpmovieplayercontroller

我试图通过这种方式使用MPMoviePlayerController重现视频:

    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()

但它从未复制过。首先,我检查了文件是否存在且存在。然后我检查了moviePlayer是否是PrepraredToPlay,但似乎在播放前没有准备好。你知道我为什么不能重现它吗?

由于

1 个答案:

答案 0 :(得分:2)

这对我有用:

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

在我的应用中,我使用全屏视频,但我已在评论全屏代码,并且我已根据您的尺寸更改了self.view.bounds:CGRectMake(0,0,200,200)。有趣的是,我也得到了未准备好的消息,但视频效果很好! 我希望这有帮助!