Use Qt for recording and releasing in Ubuntu

Source: Internet
Author: User
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:

  1. # Ifndef WIDGET_H
  2. # Define WIDGET_H
  3. # Include
  4. # Include
  5. # Include
  6. # Include
  7. Using NamespaceStd;
  8. NamespaceUi {
  9. ClassWidget;
  10. }
  11. ClassWidget:PublicQWidget
  12. {
  13. Q_OBJECT
  14. Public:
  15. ExplicitWidget (QWidget * parent = 0 );
  16. ~ Widget ();
  17. PrivateSlots:
  18. VoidStopRecording ();
  19. Private:
  20. Ui: Widget * ui;
  21. QFile outputFile;// Class member.
  22. QAudioInput * audio;// Class member.
  23. };
  24. # Endif // WIDGET_H

Widget. cpp:

  1. # Include "widget. h"
  2. # Include "ui_widget.h"
  3. Widget: Widget (QWidget * parent ):
  4. QWidget (parent ),
  5. Ui (NewUi: Widget)
  6. {
  7. Ui-> setupUi (This);
  8. OutputFile. setFileName ("/Root/qt_code/test. raw");
  9. OutputFile. open (QIODevice: WriteOnly | QIODevice: Truncate );
  10. QAudioFormat format;
  11. // Set up the format you want, eg.
  12. Format. setFrequency (8000 );
  13. Format. setChannels (1 );
  14. Format. setSampleSize (8 );
  15. Format. setCodec ("Audio/pcm");
  16. Format. setByteOrder (QAudioFormat: LittleEndian );
  17. Format. setSampleType (QAudioFormat: UnSignedInt );
  18. QAudioDeviceInfo info = QAudioDeviceInfo: defaultInputDevice ();
  19. If(! Info. isFormatSupported (format )){
  20. QWarning () <"Default format not supported try to use nearest";
  21. Format = info. nearestFormat (format );
  22. }
  23. Audio =NewQAudioInput (format,This);
  24. QTimer: singleShot (3000,This, SLOT (stopRecording ()));
  25. Audio-> start (& outputFile );
  26. // Records audio for 3000 ms
  27. Cout <"Record begin! "<Endl;
  28. }
  29. VoidWidget: stopRecording ()
  30. {
  31. Audio-> stop ();
  32. OutputFile. close ();
  33. DeleteAudio;
  34. Cout <"Record end! "<Endl;
  35. }
  36. Widget ::~ Widget ()
  37. {
  38. DeleteUi;
  39. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.