QT5オーディオ開発:QAudioOutputを使用してWAVファイルを再生し、指定されたサウンドカードを使用して再生します



Qt5 Audio Development



1.環境紹介

オペレーティングシステム:windows1064ビット

QTバージョン:5.12.6



2.機能紹介

指定されたWAVファイルを再生し、サウンドを再生するスピーカーデバイスを指定できるWAVファイルを再生するためのクラスをカプセル化します。これは、複数のサウンドカードを備えたシステムで非常に実用的です。

コードで注意すべき主なポイント: 他のシステムで実行する場合は、構造体のバイトアライメントに注意する必要があります。 WAVヘッダー構造の通常のバイトサイズは44バイトです。 LinuxシステムでWAVを再生できない場合は、WAVヘッダー構造のサイズが44バイトかどうかに注意してください。



(数字が異なるシステムでは、unsignedlong型が異なるバイトを占有します。WAVヘッダー構造にはunsignedlong型があります。他のシステムの動作に注意を払う必要がある場合は、互換性のためにunsigned intに変更できます)

3、コアコード

3.1Widget.cppファイルコード

#include 'widget.h' #include 'ui_widget.h' #include #include #include Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this) foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput)) qDebug() << 'Sound card devices available in the current system: '

3.2ウィジェット.h



#ifndef WIDGET_H #define WIDGET_H #include #include 'wav_play.h' QT_BEGIN_NAMESPACE namespace Ui { class Widget } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr) ~Widget() void wav_file_play(QString file) private slots: void on_pushButton_clicked() private: Ui::Widget *ui } #endif // WIDGET_H

3.3wav_play.cppファイルコード

#include 'wav_play.h' WAV_PLAY::WAV_PLAY(QAudioDeviceInfo info,QString wav_file) { audio=nullptr inputFile=nullptr thread=nullptr inputFile=new QFile inputFile->setFileName(wav_file) if(inputFile->open(QIODevice::ReadOnly)==false) { qDebug()start() } WAV_PLAY::~WAV_PLAY() { qDebug()close() delete inputFile } qDebug()exit() } }

3.4wav_play.hコード

#ifndef WAV_PLAY_H #define WAV_PLAY_H #include #include #include #include #include #include //WAV file structure information struct WAVFILEHEADER { // RIFF header char RiffName[4] unsigned long nRiffLength // data type identifier char WavName[4] // block header in format block char FmtName[4] unsigned long nFmtLength // Block data in the format block unsigned short nAudioFormat unsigned short nChannleNumber unsigned long nSampleRate unsigned long nBytesPerSecond unsigned short nBytesPerSample unsigned short nBitsPerSample // Block header in data block char DATANAME[4] unsigned long nDataLength } class WAV_PLAY:public QObject { Q_OBJECT public: WAV_PLAY(QAudioDeviceInfo info, QString wav_file) ~WAV_PLAY() QFile *inputFile QAudioOutput *audio QThread *thread void play() public slots: void handleStateChanged_input(QAudio::State newState) } #endif // WAV_PLAY_H

四、操作効果

インターフェースは、コードをテストするためだけに、比較的単純です。

次のパブリックアカウントには、QT C ++ C SCMチュートリアルのフルセットがあります。注意してください。