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.

Hendrik
  • 5,085
  • 24
  • 56
  • 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? – hybridcattt Oct 29 '18 at 18:50
  • I was told to set `AVURLAssetPreferPreciseDurationAndTimingKey` to `YES` when opening the asset. – Hendrik Oct 29 '18 at 21:34
  • Also filed a radar: http://www.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 come – hybridcattt Oct 30 '18 at 17:46

1 Answers1

3

Is it a bug?

Yes. See rdar://45039043.

Workaround

Use

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

instead of

CMTime cmTime = [[player currentItem] duration];
Community
  • 1
  • 1
Hendrik
  • 5,085
  • 24
  • 56