菜鸟教程小白 发表于 2022-12-13 07:58:40

ios - 在 iOS 上使用 Audio Queue 播放音频一段时间后静音


                                            <p><p>我正在编写一个 iOS 上的实时音频播放程序。</p>

<p>从peer接收音频RTP包,放入音频队列播放。</p>

<p>开始播放时,声音正常。但是 1 或 2 分钟后,声音变小了,而且 AudioQueue API 没有报错。回调函数继续正常调用,没有异常。 </p>

<p>但它只是静音了。</p>

<p>我的回调函数:</p>

<p>1:循环直到有足够的数据可以复制到音频队列缓冲区</p>

<pre><code>do
{
    read_bytes_enabled = g_audio_playback_buf.GetReadByteLen();
    if (read_bytes_enabled &gt;= kAudioQueueBufferLength)
    {
      break;
    }
    usleep(10*1000);
}
while (true);
</code></pre>

<p>2:复制到AudioQueue Buffer,并入队。这个回调函数一直正常运行,没有报错。</p>

<pre><code>//copy to audio queue buffer
read_bytes = kAudioQueueBufferLength;

g_audio_playback_buf.Read((unsigned char *)inBuffer-&gt;mAudioData, read_bytes);

WriteLog(LOG_PHONE_DEBUG, &#34;AudioQueueBuffer(Play): copy [%d] bytes to AudioQueue buffer! Total len = %d&#34;, read_bytes, read_bytes_enabled);

inBuffer-&gt;mAudioDataByteSize = read_bytes;

UInt32 nPackets = read_bytes / g_audio_periodsize; // mono

inBuffer-&gt;mPacketDescriptionCount = nPackets;

// re-enqueue this buffer
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题已解决。</p>

<p>关键是,你不能让音频队列缓冲区等待,你必须一直喂它,否则它可能会被静音。如果你没有足够的数据,用空白数据填充它。</p>

<p>所以下面的代码应该改一下:</p>

<pre><code>do
{
    read_bytes_enabled = g_audio_playback_buf.GetReadByteLen();
    if (read_bytes_enabled &gt;= kAudioQueueBufferLength)
{
    break;
}
    usleep(10*1000);
}
while (true);
</code></pre>

<p>改成这样:</p>

<pre><code>read_bytes_enabled = g_audio_playback_buf.GetReadByteLen();
if (read_bytes_enabled &lt; kAudioQueueBufferLength)
{
    memset(inBuffer-&gt;mAudioData, 0x00, kAudioQueueBufferLength);
}
else
{
    inBuffer-&gt;mAudioDataByteSize = kAudioQueueBufferLength;
}
...
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 iOS 上使用 Audio Queue 播放音频一段时间后静音,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/30794479/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/30794479/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 iOS 上使用 Audio Queue 播放音频一段时间后静音