Environment: Ubuntu10.04, QtCreator2.0.1, and Qt4.7.0 refer to the Qt help documentation to operate on QAudioInput and QAudioOutput to implement recording and playing. workflow: test_record_ver0 engineering recording 3 S, save the audio file in/root/qt_code/test. in raw, The test_play_ver0 project opens test. raw file and output sound. source code:
Environment: Ubuntu 10.04, Qt Creator 2.0.1, and Qt 4.7.0
Refer to the Qt help document to operate the QAudioInput class and QAudioOutput class for recording and playing.
Workflow: test_record_ver0 engineering recording 3 S. Save the sound file in/root/qt_code/test. raw. Open the test. raw file in the test_play_ver0 project and output the sound.
Source code:
Test_record_ver0 project:
Widget. h:
- # Ifndef WIDGET_H
- # Define WIDGET_H
- # Include
- # Include
- # Include
- # Include
- Using NamespaceStd;
- NamespaceUi {
- ClassWidget;
- }
- ClassWidget:PublicQWidget
- {
- Q_OBJECT
- Public:
- ExplicitWidget (QWidget * parent = 0 );
- ~ Widget ();
- PrivateSlots:
- VoidStopRecording ();
- 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 (NewUi: 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 =NewQAudioInput (format,This);
- QTimer: singleShot (3000,This, SLOT (stopRecording ()));
- Audio-> start (& outputFile );
- // Records audio for 3000 ms
- Cout <"Record begin! "<Endl;
- }
- VoidWidget: stopRecording ()
- {
- Audio-> stop ();
- OutputFile. close ();
- DeleteAudio;
- Cout <"Record end! "<Endl;
- }
- Widget ::~ Widget ()
- {
- DeleteUi;
- }