[AVPlayerItem duration] is always indefinite

Originator:hendrik.schreiber
Number:rdar://45039043 Date Originated:10/5/2018
Status:Open Resolved:
Product:macOS + SDK Product Version:10.14.0 (18A391)
Classification:Bug Reproducible:Always
 
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.

macOS 10.14.0 (18A391)

Comments

Workaround

Use

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

instead of

CMTime cmTime = [[player currentItem] duration];

By hendrik.schreiber at Oct. 5, 2018, 2:48 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!