2

我正在连接 2 个音频文件。然后我通过 AVAudioPlayer 播放该音频文件。连接的文件播放完美。但是,当我想获取 audioPlayer 的持续时间时,它会显示错误的持续时间,即 2 秒。但我连接的音频文件的实际持续时间是 7 秒。问题出在哪里?提前感谢您的帮助。

-(void)playAudio{

part1 = [NSMutableData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Mumu night its getting dark" ofType: @"mp3"]];

part2 = [NSMutableData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Mumu night did you study" ofType: @"mp3"]];

[part1 appendData: part2];

audioPlayer=[[AVAudioPlayer alloc] initWithData:part1 error:NULL];

audioPlayer.delegate = self;

[audioPlayer play];

duration = (int)audioPlayer.duration;

NSLog(@"audio duration: %d", duration);

}
4

2 回答 2

0

player.prepareToPlay()在获得持续时间之前尝试调用。它将向您显示更准确的持续时间。

为了完全准确,请尝试:

CMTimeGetSeconds(AVURLAsset.init(url: myURL, options: nil).duration)
于 2021-03-25T02:21:15.190 回答
-1

你应该使用这个

https://developer.apple.com/documentation/avfoundation/avasynchronouskeyvalueloading/1387321-loadvaluesasynchronously

在示例中,只需将“commonMetadata”更改为“duration”。

于 2021-03-25T02:24:01.287 回答