我正在尝试使用以下代码读取本地存储的音频文件的持续时间:
#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");
}
代码是not在主 AppKit 线程中执行,它曾经在 macOS 10.13.x 及更早版本下工作。现在有10.14.0了always报告"Duration is kCMTimeIndefinite"
。即使在我开始播放该文件之后。
有人可以吗:
- 确认/否认这是 macOS 10.14.0 中的错误
- 建议一个解决方法
Thanks.
这是一个错误吗?
是的。看雷达://45039043 https://openradar.appspot.com/radar?id=4968600712511488.
解决方法
Use
CMTime cmTime = [[[player currentItem] asset] duration];
代替
CMTime cmTime = [[player currentItem] duration];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)