视频不能在MPMoviePlayerController上播放

问题描述

| 我想获取iPhone中视频的总时长。我正在使用以下代码获取持续时间
videoPath = [videoPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:videoPath];
MPMoviePlayerController *mpPlayer = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease]; 
mpPlayer.view.frame = CGRectMake(0,320,100);
[self.view addSubview:mpPlayer.view];
NSLog(@\"duration = %f\",mpPlayer.duration);
[mpPlayer play];
但我不知道为什么我无法获取视频的时长,也无法播放视频。 谁能帮助我找到解决此问题的方法。 提前致谢。

解决方法

提示: 持续时间可能在实际播放之前不可用->收到
MPMoviePlayerPlaybackStateDidChangeNotification
(出于安全考虑,the2ѭ)时,请再次检查持续时间。,请检查视频路径是否正确,并检查播放器是否分配了正确的文件网址,
    videoPath = [videoPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:videoPath];
    MPMoviePlayerController *mpPlayer = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease]; 
    mpPlayer.view.frame = CGRectMake(0,320,100);
    [self.view addSubview:mpPlayer.view];
    NSLog(@\"duration = %f\",mpPlayer.duration);
    [mpPlayer play];

//add notifications


  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {

    if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
duration = mpPlayer.duration;
NSLog(@\"duration = %f\",mpPlayer.duration);
}
}       

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...