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

1

2

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.

Smug answered 5/10, 2018 at 11:2 Comment(3)
I am experiencing a similar issue on iOS - rarely duration becomes available after the item becomes readyToPlay (even when using AVPlayerItem(asset: asset, automaticallyLoadedAssetKeys: "duration"). Have you received any feedback on your radar?Nightmare
I was told to set AVURLAssetPreferPreciseDurationAndTimingKey to YES when opening the asset.Smug
Also filed a radar: openradar.me/45672773. My files are remote and very long, so AVURLAssetPreferPreciseDurationAndTimingKey can decrease performance. Made a workaround for now to wait for duration KVO notification to comeNightmare
S
3

Is it a bug?

Yes. See rdar://45039043.

Workaround

Use

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

instead of

CMTime cmTime = [[player currentItem] duration];
Smug answered 5/10, 2018 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.