Mojave/macOS 10.14.0: [AVPlayerItem duration] is always indefinite

我的梦境 提交于 2021-01-28 02:26:06

问题


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");
}

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.

Can someone please:

  1. confirm/deny this is a bug in macOS 10.14.0
  2. suggest a workaround

Thanks.


回答1:


Is it a bug?

Yes. See rdar://45039043.

Workaround

Use

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

instead of

CMTime cmTime = [[player currentItem] duration];


来源:https://stackoverflow.com/questions/52664180/mojave-macos-10-14-0-avplayeritem-duration-is-always-indefinite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!