首页 > 解决方案 > AudioToolbox ExtAudioFileSeek(_:_:) does not directly set the desired frame position

问题描述

I am working on an iOS MPEG-DASH player and I have an issue with seeking feature.

ExtAudioFileSeek(::) documentation said

Sets the file’s read position to the specified sample frame number. A subsequent call to the ExtAudioFileRead(::_:) function returns samples from precisely this location, even if it is located in the middle of a packet.

Unfortunately, the AudioToolbox.AudioFile_ReadProc loop does not seek straight to the right frame and goes trough all file segments, requesting 16 and 27682 bytes packages. It takes a lot of time (especially for long tracks) and force to download all intermediate segments (that should not be required) This also cause the app to crash when long tracks with high sound quality are being played.

Here my log trace. I converted frame index to "mega" for more readability.

[PlayerEngine] - Trying to seek at 16.870537 % 
[AudioSource] - Pausing AudioOutputUnit 
[AudioSource] - AudioOutputUnit successfully stopped 
[AudioOutputUnit] - Trying to seek at frame 31.006805M 
[AudioOutputUnit] - Successfully sought at frame 31.006805M 

[AudioInputUnit] - Audio frames have been flushed 
[AudioInputUnit] - Seek to frame 31.006806M pending 
[AudioConverter] - Converted frame buffer has been flush 
[AudioSource] - Resuming AudioOutputUnit 

[AudioSource] - AudioOutputUnit successfully resumed 
[PlayerEngine] - Successfully sought at 16.870537 % 
[AudioInputUnit] - Seeking to frame 31.006805M 
[CoreAudioDecoder] - Trying to seek at frame 31.006805M 
[CoreAudioDecoder] - Seek on frame 31.006806M done successfully 

[CoreAudioDecoder] - AudioToolbox.AudioFile_ReadProc : inClientData, inPosition:1.315635M, requestCount:16
(...)
[CoreAudioDecoder] - AudioToolbox.AudioFile_ReadProc : inClientData, inPosition:1.331687M, requestCount:27682

Is it a bug on AudioToolBox or is there a way to fix it ?

Thanks a lot !

标签: iosaudioavfoundationcore-audioaudiotoolbox

解决方案


凉爽的!

由于您提到读取回调,我假设您不仅使用ExtAudioFileAPI,还使用AudioFile​​API,例如ExtAudioFileWrapAudioFileID(AudioFileInitializeWithCallbacks(...))

压缩的音频格式并不总是在帧和文件偏移之间具有简单的映射,因此您看到的幼稚行为可能是由于这些 API 之一(AudioFile?)可以理解的是不知道这种映射。

尝试在或包装的音频文件上设置kExtAudioFileProperty_PacketTable属性。前者可能更有意义。我不知道整个数据包表信息是否从一开始就可供您使用,或者是否会随着时间的推移而显示给您,也不知道 API 将如何对您多次设置这些属性做出反应。ExtAudioFilekAudioFilePropertyPacketTableInfo

祝你好运!


推荐阅读