??
Qt synchronized recording playback under Windows
this blog link: http://blog.csdn.net/jdh99 , Author: jdh, reprint please specify.
Environment:
Host: WIN8
Development environment: QT5 3.1.2
Description
Do a QQ-like voice transmission software, so test how to use QT for voice synchronization playback under Windows
Source:
Mainwindow.h
#ifndef mainwindow_h#define mainwindow_h#include <QMainWindow> #include <QAudioInput> #include < qaudiooutput> #include <iostream> #include <QFile> #include <qbuffer>namespace Ui {class MainWindow;} Class Mainwindow:public qmainwindow{ q_objectpublic: explicit MainWindow (Qwidget *parent = 0); ~mainwindow ();p rivate slots: void stoprecording (); void Finishedplaying (qaudio::state state); void Readmore ();p rivate: ui::mainwindow *ui; QFile OutputFile; Class member. qaudioinput* audio_in; Class member. QFile Inputfile; Class member. qaudiooutput* audio_out; Class member. Qiodevice *mybuffer_in; Qiodevice *mybuffer_out; Qbuffer Buffer;}; #endif//Mainwindow_h
Mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <qtimer>mainwindow:: MainWindow (Qwidget *parent): Qmainwindow (parent), UI (new Ui::mainwindow) {ui->setupui (this); Outputfile.setfilename ("Test.raw"); Outputfile.open (Qiodevice::writeonly | Qiodevice::truncate); Qaudioformat format; Set up the format you want, eg. Format.setsamplerate (8000); Format.setchannelcount (1); Format.setsamplesize (16); Format.setcodec ("AUDIO/PCM"); Format.setbyteorder (Qaudioformat::littleendian); Format.setsampletype (Qaudioformat::unsignedint); Format.setsampletype (Qaudioformat::signedint); Qaudiodeviceinfo info = qaudiodeviceinfo::d efaultinputdevice (); if (!info.isformatsupported (format)) {qwarning () << "default format not supported try-to-use nearest"; format = info.nearestformat (format); } audio_in = new Qaudioinput (format, this); Qtimer::singleshot (, this, SLOT (Stoprecording ())); //audio_in->start (&outputfile); mybuffer_in = Audio_in->start (); Connect (mybuffer_in, SIGNAL (Readyread ()), SLOT (Readmore ())); Records Audio for 3000ms qdebug () << "Record begin!" << Endl; Inputfile.setfilename ("Test.raw"); Inputfile.open (qiodevice::readonly); Audio_out = new Qaudiooutput (format, this); Connect (audio_out,signal (statechanged (qaudio::state)), SLOT (finishedplaying (qaudio::state))); Audio_out->start (&inputfile); Audio_out->start (mybuffer_out); Mybuffer_out = Audio_out->start (); Qdebug () << "Play begin!" << Endl;} Mainwindow::~mainwindow () {Delete UI;} void Mainwindow::readmore () {if (!audio_in) return; Qbytearray M_buffer (2048,0); Qint64 len = Audio_in->bytesready (); Qdebug () << "len1 =" << len; Qint64 L = mybuffer_in->read (M_buffer.data (), Len); Qdebug () << "len2 =" << l; Mybuffer_out->write (M_buffer);} void Mainwindow::stopreCording () {qdebug () << "Enter Stop"; Audio_in->stop (); Outputfile.close (); Delete audio_in; Qdebug () << "Record end!" << endl;//inputfile.setfilename ("Test.raw");//Inputfile.open (Qiodevice:: READONLY);//Qaudioformat format;////Set up the format, eg.//format.setsamplerate (8000);//Format.setchannelcou NT (1);//Format.setsamplesize (+)//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_out = new Qaudiooutput (format, this);//Connect (audio_out,signal (statechanged (qaudio::state ), SLOT (finishedplaying (qaudio::state)));//Audio_out->start (&inputfile);////audio_out->start ( Mybuffer);//Qdebug () << "Play Begin"! "<< Endl; }void mainwindow::finishedplaying (qaudio::state State) {//if (state = = Qaudio::idlestate) {//Audio_out->stop (); /Inputfile.close ();//Delete audio_out;//} qdebug () << "Play end!" << Endl; }
Main.cpp
#include "mainwindow.h" #include <qapplication>int main (int argc, char *argv[]) { qapplication A (argc, argv); C7/>mainwindow W; W.show (); return a.exec ();}
Qt synchronized recording playback under Windows