gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:25:25 25 4
gpt4 key购买 nike

我正在尝试使用以下代码读取本地存储的音频文件的持续时间:

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

该代码不在主 AppKit 线程中执行,它曾经在 macOS 10.13.x 及更早版本下工作。现在 10.14.0 总是报告 "Duration is kCMTimeIndefinite" .即使在我开始播放文件之后。

有人可以请:
  • 确认/否认这是 macOS 10.14.0 中的错误
  • 建议解决方法

  • 谢谢。

    最佳答案

    这是一个错误吗?
    是的。见 rdar://45039043 .
    解决方法

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

    关于objective-c - Mojave /macOS 10.14.0 : [AVPlayerItem duration] is always indefinite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52664180/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com