用Qt实现录音以及放音

本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.

环境:Ubuntu10.04,Qt Creator 2.0.1,Qt 4.7.0

 

参考Qt帮助文档,对QAudioInput类以及QAudioOutput类进行操作,实现录音以及放音.

 

工作流程:test_record_ver0工程录音3S,将声音文件保存在/root/qt_code/test.raw中,test_play_ver0工程打开test.raw文件,并输出声音.

 

源代码:

test_record_ver0工程:

widget.h:

#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QtGui> #include <QAudioInput> #include <iostream> using namespace std; namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void stopRecording(); private: Ui::Widget *ui; QFile outputFile; // class member. QAudioInput* audio; // class member. }; #endif // WIDGET_H  

 

widget.cpp:

#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); outputFile.setFileName("/root/qt_code/test.raw"); outputFile.open( QIODevice::WriteOnly | QIODevice::Truncate ); QAudioFormat format; // set up the format you want, eg. format.setFrequency(8000); format.setChannels(1); format.setSampleSize(8); format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::UnSignedInt); QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice(); if (!info.isFormatSupported(format)) { qWarning()<<"default format not supported try to use nearest"; format = info.nearestFormat(format); } audio = new QAudioInput(format, this); QTimer::singleShot(3000, this, SLOT(stopRecording())); audio->start(&outputFile); // Records audio for 3000ms cout <<"record begin!" << endl; } void Widget::stopRecording() { audio->stop(); outputFile.close(); delete audio; cout <<"record end!" << endl; } Widget::~Widget() { delete ui; }  

 

test_play_ver0工程:

widget.h:

#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QtGui> #include <QAudioInput> #include <QAudioOutput> #include <iostream> using namespace std; namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void finishedPlaying(QAudio::State state); private: Ui::Widget *ui; QFile inputFile; // class member. QAudioOutput* audio; // class member. }; #endif // WIDGET_H  

 

wideget.cpp:

#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); inputFile.setFileName("/root/qt_code/test.raw"); //inputFile.setFileName("/root/share/wav.wav"); inputFile.open(QIODevice::ReadOnly); QAudioFormat format; // Set up the format, eg. format.setFrequency(8000); format.setChannels(1); format.setSampleSize(8); format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::UnSignedInt); QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); if (!info.isFormatSupported(format)) { qWarning()<<"raw audio format not supported by backend, cannot play audio."; return; } audio = new QAudioOutput(format, this); connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State))); audio->start(&inputFile); cout <<"play begin!" << endl; } void Widget::finishedPlaying(QAudio::State state) { if(state == QAudio::IdleState) { audio->stop(); inputFile.close(); delete audio; } cout << "play end!" << endl; } Widget::~Widget() { delete ui; }  

 



 

你可能感兴趣的:(UI,delete,Class,qt,audio,Signal)