QT QAudioOutput+QIODevice 音频流实时播放
生活随笔
收集整理的這篇文章主要介紹了
QT QAudioOutput+QIODevice 音频流实时播放
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
頭文件
class QByteArray; class QAudioOutput; class QIODevice; class CAudioMonitorThread : public QThread {Q_OBJECTpublic:CAudioMonitorThread(QObject *parent = nullptr);~CAudioMonitorThread();void Stop(); protected:void run() override; private slots:void OnstateChanged(QAudio::State); private:bool m_bStop = false;std::shared_ptr<QBuffer> m_audioBuffer = nullptr;std::shared_ptr<QByteArray> m_pByte = nullptr;std::shared_ptr<QAudioOutput> out = nullptr;QIODevice *io = nullptr; };實現:
#include "CAudioMonitorThread.h" #include "CCommandInterface.h" #include "CMutilQueues.h" #include <QUrl> #include <QBuffer> #include <QAudioFormat> #include <QAudioOutput> #include "CGoogleLog.h" CAudioMonitorThread::CAudioMonitorThread(QObject *parent): QThread(parent) {m_pByte.reset(new QByteArray);m_audioBuffer.reset(new QBuffer(m_pByte.get()));QAudioFormat fmt; //通過fmt設定音頻數據格式。只有明確知道音頻數據的聲道數、采樣率、采樣位數,才可以正常地播放fmt.setSampleRate(44100); //設定播放采樣頻率為44100Hz的音頻文件fmt.setSampleSize(16); //設定播放采樣格式(采樣位數)為16位(bit)的音頻文件。QAudioFormat支持的有8/16bit,即將聲音振幅化為256/64k個等級fmt.setChannelCount(1); //設定播放聲道數目為2通道(立體聲)的音頻文件。mono(平聲道)的聲道數目是1,stero(立體聲)的聲道數目是2fmt.setCodec("audio/pcm"); //播放PCM數據(裸流)得設置編碼器為"audio/pcm"。"audio/pcm"在所有的平臺都支持,也就相當于音頻格式的WAV,以線性方式無壓縮的記錄捕捉到的數據。如想使用其他編碼格式 ,可以通過QAudioDeviceInfo::supportedCodecs()來獲取當前平臺支持的編碼格式fmt.setByteOrder(QAudioFormat::LittleEndian); //設定字節序,以小端模式播放音頻文件fmt.setSampleType(QAudioFormat::UnSignedInt); //設定采樣類型。根據采樣位數來設定。采樣位數為8或16位則設置為QAudioFormat::UnSignedIntout.reset(new QAudioOutput(fmt));io = out->start(); //調用start函數后,返回QIODevice對象的地址io->open(QIODevice::ReadWrite);connect(out.get(), SIGNAL(stateChanged(QAudio::State)), this, SLOT(OnstateChanged(QAudio::State))); }CAudioMonitorThread::~CAudioMonitorThread() { }void CAudioMonitorThread::Stop() {m_bStop = true; }void CAudioMonitorThread::run() {int size = out->periodSize();std::shared_ptr<char> buffer(new char[size], std::default_delete<char[]>());memset(buffer.get(), 0x00, size);while (!m_bStop){//獲取音頻信息CCommandAudio * pCommand = (CCommandAudio*)CMutilQueues::GetData(EQueueId::AUDIO);if (pCommand == nullptr){continue;}//放入buffQByteArray byt = pCommand->GetResult();m_audioBuffer->open(QIODevice::ReadWrite);m_audioBuffer->write(byt);m_audioBuffer->seek(0);//讀取buff送入聲卡while (!m_audioBuffer->atEnd()){memset(buffer.get(), 0x00, size);if (out->bytesFree() == 0)//聲卡緩沖區無空閑時不寫數據,跳過{continue;}if (m_audioBuffer->read(buffer.get(), size) <= 0) //從緩沖據讀取數據 ,如果讀取到文件末尾或者讀取不成功則通過break函數跳出while循環{break;}io->write(buffer.get(), size); //調用write函數將內存buf中的PCM數據寫入到揚聲器,即把buf中的數據提交到聲卡發聲}m_audioBuffer->close();//沒有進入消息循環的event需要手動釋放delete pCommand;pCommand = nullptr;}io->close(); }void CAudioMonitorThread::OnstateChanged(QAudio::State newState) {switch (newState){case QAudio::IdleState:LOG(INFO) << "QAudio::IdleState";break;case QAudio::StoppedState:LOG(INFO) << "QAudio::StoppedState";break;case QAudio::SuspendedState:LOG(INFO) << "QAudio::SuspendedState";break;case QAudio::ActiveState:LOG(INFO) << "QAudio::ActiveState";break;default:break;} }總結
以上是生活随笔為你收集整理的QT QAudioOutput+QIODevice 音频流实时播放的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创客匠人工具助力教培机构快速适应线上教学
- 下一篇: SketchUp Pro 2017 简体