Mojave / macOS 10.14.0:[AVPlayerItem持续时间]总是不确定的

时间:2018-10-05 11:02:53

标签: objective-c audio avplayer avplayeritem macos-mojave

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

#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"。即使在我开始播放文件之后。

有人可以请

  1. 确认/否认这是macOS 10.14.0中的错误
  2. 建议解决方法

谢谢。

1 个答案:

答案 0 :(得分:1)

是错误吗?

是的。参见rdar://45039043

解决方法

使用

 SELECT Z.Trade_Date
     , sum(case when name = 'Port A' then P_RETURN end) as PortA
     , sum(case when name = 'Port B' then P_RETURN end) as PortB
FROM (
## Raw data
SELECT CurDay.*, NextDay.Price/CurDay.Price*CurDay.Weight/CurDay.Inst_Total_Weight as P_Return
FROM (SELECT x1.*, @RN:=@RN+1 rn,x2.inst_cnt, x2.Inst_Total_Weight
      FROM (SELECT prt.name, W.port_ID, W.inst_ID, W.weight, prc.trade_Date, Prc.Price
            FROM x_ports Prt
            INNER JOIN x_weights W
              on W.Port_ID = prt.ID
            INNER JOIN x_prices Prc
              on Prc.INST_ID = W.INST_ID
            ORDER BY W.port_id, W.inst_id,trade_Date) x1
     CROSS join (SELECT @RN:=0) r
     INNER join (SELECT count(*) inst_Cnt, port_ID, sum(Weight) as Inst_Total_Weight 
                 FROM x_weights
                 GROUP BY Port_ID) x2
        on X1.Port_ID = X2.Port_ID) CurDay
LEFT JOIN (SELECT x1.*, @RN2:=@RN2+1 rn2
           FROM (SELECT prt.name, W.port_ID, W.inst_ID, W.weight, prc.trade_Date, Prc.Price
                 FROM x_ports Prt
                 INNER JOIN x_weights W
                   on W.Port_ID = prt.ID
                 INNER JOIN x_prices Prc
                   on Prc.INST_ID = W.INST_ID
                 ORDER BY W.port_id, W.inst_id,trade_Date) x1
                 CROSS join (SELECT @RN2:=0) r
           ) NextDay
   on NextDay.Port_ID = CurDay.Port_ID
  and NextDay.Inst_ID = curday.Inst_ID
  and NextDay.RN2 = CurDay.RN+1
GROUP BY CurDay.Port_ID,  CurDay.Inst_ID,  CurDay.Trade_Date) Z
##END RAW DATA
GROUP BY Trade_Date;




+----+---------------------+-------------------+-------------------+
|    |     Trade_Date      |       PortA       |       PortB       |
+----+---------------------+-------------------+-------------------+
|  1 | 01.01.2018 00:00:00 | 1,00528959642786  | 1,00892857142857  |
|  2 | 02.01.2018 00:00:00 | 0,995851495829569 | 0,991150442477876 |
|  3 | 03.01.2018 00:00:00 | 0,999840954274354 | 1                 |
|  4 | 04.01.2018 00:00:00 | 1,0035355651507   | 1,00892857142857  |
|  5 | 05.01.2018 00:00:00 | 1,00589689563141  | 1,00884955752212  |
|  6 | 06.01.2018 00:00:00 | NULL              | NULL              |
+----+---------------------+-------------------+-------------------+

代替

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