首页 > 代码库 > qt在windows下实现录音放音同步

qt在windows下实现录音放音同步

??

qt在windows下实现录音放音同步


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


环境:

主机:WIN8

开发环境:Qt5 3.1.2


说明:

做一个类似qq的语音传输的软件,所以测试windows下如何用qt进行语音同步播放


源码:

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_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void stopRecording();
    void finishedPlaying(QAudio::State state);
    void readMore();
private:
    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::defaultInputDevice();
    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(3000, 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.setChannelCount(1);
//   format.setSampleSize(16);
//   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);
    MainWindow w;
    w.show();

    return a.exec();
}

qt在windows下实现录音放音同步