简体   繁体   English

Qt-如何设置从缓冲区开头开始的音频播放?

[英]Qt - how to set audio playback starts from the beginning of buffer?

I have a buffer with a size for example 4096, and store data into it, if the buffer is full, it would be start from the beginning of the buffer. 我有一个大小例如为4096的缓冲区,并将数据存储到其中,如果缓冲区已满,它将从缓冲区的开头开始。 It seems like this works fine. 看来这工作正常。

But I have problem with playing data from buffer. 但是我从缓冲区播放数据有问题。

QByteArray          m_buffer;
QBuffer             m_audioOutputIODevice;
QAudioOutput*       m_audioOutput;
m_audioOutputIODevice.close();
m_audioOutputIODevice.setBuffer(&m_buffer);
m_audioOutputIODevice.open(QIODevice::ReadOnly);
m_audioOutput->start(&m_audioOutputIODevice);

Now I can play the sound from the buffer but when it reaches the end of buffer, play stops. 现在,我可以播放缓冲区中的声音,但是当声音到达缓冲区的尽头时,播放停止。 How could I change the code so when it reaches the end of buffer it would all start from the beginning? 我该如何更改代码,以便当它到达缓冲区的末尾时都从头开始? Thank you very much 非常感谢你

update codes: 更新代码:

connect(m_audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(resetPlayBuffer(QAudio::State)));

    void bufferPlayback::resetPlayBuffer (QAudio::State state)
{
    if (state == QAudio::IdleState) {
        m_audioOutputIODevice.close();
        m_audioOutputIODevice.setBuffer(&m_buffer);
        m_audioOutputIODevice.open(QIODevice::ReadOnly);
    }
}

void stateChanged ( QAudio::State state ) <~ signal for when the player changes. void stateChanged ( QAudio::State state ) <〜播放器更改时的信号。 Hook to a slot in your class, and just repeat the playback process when the state is stopped. 钩到班级中的某个插槽,然后在状态停止时重复播放过程。 Simple. 简单。 One of the reasons I LOVE Qt. 我爱Qt的原因之一。

AFAICT QAudioOutput doesn't have any built-in support for audio looping, so I think you would have to simulate audio-looping by sending fresh audio buffers to the QAudioOutput device periodically so that it would never run out of audio bytes to play. AFAICT QAudioOutput没有对音频循环的任何内置支持,因此我认为您必须通过定期向QAudioOutput设备发送新的音频缓冲区来模拟音频循环,以使其永远不会耗尽音频字节来播放。

I think the easiest way to do that would be to write your own subclass of QIODevice that pretends to be a very long (infinite?) file, but returns your looping samples over and over again when queried. 我认为最简单的方法是编写自己的QIODevice子类,该子类伪装成一个很长的(无限?)文件,但在查询时一遍又一遍地返回循环样本。 Then pass your QIODevice-subclass-obect as an argument to QAudioOutput::start(). 然后将您的QIODevice-subclass-obect作为参数传递给QAudioOutput :: start()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM