【问题标题】:player.duration shows always zero in MPMoviePlayerController for video fileplayer.duration 在视频文件的 MPMoviePlayerController 中始终显示为零
【发布时间】:2012-01-27 15:46:15
【问题描述】:

我正在使用此代码在 mpmediaplayer 控制器中播放一个小视频

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] 
                                   initWithContentURL:[NSURL fileURLWithPath:videostr]]; 

videostr 是该视频文件的路径。

现在我需要获取我正在使用此代码的视频文件的长度。

length = player.duration;

但它总是显示 0.000。但是视频播放得很好。

我正在通过谷歌搜索获取视频持续时间的每个地方的代码仅player.duration 进行检查。

我也尝试了一些其他代码

AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videostr] 
                                             options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], AVURLAssetPreferPreciseDurationAndTimingKey, nil]] autorelease];
NSTimeInterval duration; 
if (asset) 
    duration = CMTimeGetSeconds(asset.duration) ;
NSLog(@">>>>>>>>>>>>>>>>>>>> %f", asset.duration);

即使它显示为零。谁能帮帮我。

提前致谢。

【问题讨论】:

    标签: iphone ios mpmovieplayercontroller


    【解决方案1】:

    在内容真正可播放之前,您无法获得有用的持续时间。

    注册负载状态变化:

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

    收到通知后评估状态:

    - (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification
    {
        if ((player.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK)
        {
            NSLog(@"content play length is %g seconds", player.duration);
        }
    }
    

    【讨论】:

    • 通知:MPMoviePlayerLoadStateDidChangeNotification 自动调用 2 次​​span>
    • 如果默认情况下视频被暂停此解决方案不起作用,因为当用户执行播放操作时会调用此通知。是否存在任何方式来获取之前的持续时间?
    【解决方案2】:

    斯威夫特

    你可以在 swift 中这样做

    func getMediaDuration(url: NSURL!) -> Float64{
            var asset : AVURLAsset = AVURLAsset.assetWithURL(url) as AVURLAsset
            var duration : CMTime = asset.duration
            
            return CMTimeGetSeconds(duration)
        }
    

    【讨论】:

      【解决方案3】:

      使用 MPMovieDurationAvailableNotification

      [[NSNotificationCenter defaultCenter] addObserver:self 
                                           selector:@selector(movieDurationAvailable:) 
                                               MPMovieDurationAvailableNotification object:nil];
      
      - (void)movieDurationAvailable:(NSNotification *)notification {
      
              NSLog(@"content play length is %g seconds", moviePlayer.duration);
      
      
       }
      

      【讨论】:

      • 也许可以稍微扩展一下这个答案。带有代码示例或有用的东西......
      【解决方案4】:

      您可以使用以下代码从视频文件中获取持续时间。

      NSURL *videoURL = [NSURL fileURLWithPath:VideoFileURL];
      
      AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
      
      CMTime duration = sourceAsset.duration;
      
      double durationInSeconds = duration.value/duration.timescale;
      

      durationInSeconds 变量中,您可以得到确切的持续时间(以秒为单位)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-25
        • 2017-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-02
        • 2021-05-21
        • 2017-01-09
        相关资源
        最近更新 更多