音频播放器播放时长在播放时发生变化

问题描述 投票:0回答:3

当我在播放音频文件之前获取其持续时间时:

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
...
[audioPlayer prepareToPlay];
duration = audioPlayer.duration;

我得到的持续时间例如为 16.71 秒。然后,当播放时每 0.2 秒采样一次时,持续时间会发生变化。它每 1.2 到 1.6 秒更改为:16.71s、17.02s、17.23s、17.33s、17.38s、17.43s、17.38s、17.29s、17.31s,然后在最后 2.5 秒保持在 17.51s。所以它会上升和下降。

我在更新滑块位置的方法中进行采样,并显示经过的时间和总持续时间。您可以想象,在玩游戏时看到持续时间(经过截断)从 16 变为 17,看起来真的很奇怪。此外,滑块位置将随着所有这些漂移而偏离。

有人知道这是为什么吗?

现在我们正在讨论持续时间:有谁知道为什么当省略

audio player.duration
时,
[audioPlayer prepareToPlay]
可以返回大约是实际持续时间两倍的值?

iphone ios avaudioplayer
3个回答
6
投票

avaudioplayer 的持续时间方法返回的持续时间是根据到目前为止已解码的文件量对文件总持续时间的最佳估计。这就是为什么当您随着时间的推移检查持续时间时,持续时间会不断得到完善。

为了获得更好的时间,我使用AVAsset的duration方法。它更好地解释了这里发生的事情:

https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAsset_Class/Reference/Reference.html#//apple_ref/occ/instp/AVAsset/duration

如果您指定providesPreciseDurationAndTiming = YES,那么AVAsset将在需要时解码文件以确定其准确的持续时间。如果解码时间太长,您可以禁用它。

在我的情况下,我使用 AVURLAsset 子类:

            AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:localURL options:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], AVURLAssetPreferPreciseDurationAndTimingKey, nil]];
            float t = CMTimeGetSeconds(asset.duration);

2
投票

@AndyKorth 的 answer 是最好的!这是 Swift 中的

guard let audioPlayer = audioPlayer, let url = audioPlayer.url else { return }

let assetOpts = [AVURLAssetPreferPreciseDurationAndTimingKey: true]
let asset = AVURLAsset(url: url, options: assetOpts)

let assetDuration: Float64 = CMTimeGetSeconds(asset.duration)

// compare both
print("assetDuration: ", assetDuration)

print("audioPlayerDuration: ", Float(audioPlayer.duration))

0
投票

如果您是从自己身边录制音频,您可以将音频文件时长放入网络参数中,以便在从网络下载音频文件时在音频播放中使用。

© www.soinside.com 2019 - 2024. All rights reserved.