Mojave/macOS 10.14.0:[AVPlayerItem持续时间]始终是不确定的 [英] Mojave/macOS 10.14.0: [AVPlayerItem duration] is always indefinite

查看:73
本文介绍了Mojave/macOS 10.14.0:[AVPlayerItem持续时间]始终是不确定的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码读取本地存储的音频文件的持续时间:

I'm trying to read the duration of a locally stored audio file using the following code:

#import <Foundation/Foundation.h>
#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>

AVPlayer *player = [AVPlayer playerWithURL: urlForLocalAudioFile];
// busy wait - I know, not elegant, please ignore
int timeout = 0;
while (([player status] == AVPlayerStatusUnknown
        || [[player currentItem] status] == AVPlayerItemStatusUnknown)
        && timeout < 100) {
    [NSThread sleepForTimeInterval: 0.1];
    timeout++;
}
// make sure we have the right status
if ([[player currentItem] status] == AVPlayerItemStatusReadyToPlay) {
    CMTime cmTime = [[player currentItem] duration];
    if (CMTIME_IS_INDEFINITE(cmTime)) {
        NSLog(@"Duration is kCMTimeIndefinite");
    } else {
        NSLog(@"Time: %d", CMTimeGetSeconds(cmTime));
    }
} else {
    NSLog(@"Item not ready to play");
}

该代码不是在AppKit主线程中执行的,它以前可在macOS 10.13.x及更低版本下运行.现在,在10.14.0中,它总是报告持续时间为kCMTimeIndefinite" .即使在我开始播放文件之后.

The code is not executed in the main AppKit thread and it used to work under macOS 10.13.x and earlier. Now with 10.14.0 it always reports "Duration is kCMTimeIndefinite". Even after I have started playing the file.

有人可以请

  1. 确认/否认这是macOS 10.14.0中的错误
  2. 建议解决方法

谢谢.

推荐答案

这是一个错误吗?

是的.参见 rdar://45039043 .

使用

CMTime cmTime = [[[player currentItem] asset] duration];

代替

CMTime cmTime = [[player currentItem] duration];

这篇关于Mojave/macOS 10.14.0:[AVPlayerItem持续时间]始终是不确定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆