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:
- confirm/deny this is a bug in macOS 10.14.0
- suggest a workaround
Thanks.
AVPlayerItem(asset: asset, automaticallyLoadedAssetKeys: "duration"
). Have you received any feedback on your radar? – NightmareAVURLAssetPreferPreciseDurationAndTimingKey
toYES
when opening the asset. – Smug