qt windows 에서 녹음 재생 동기 화 실현


qt windows 에서 녹음 재생 동기 화 실현
본문 블 로그 링크:http://blog.csdn.net/jdh99, 저자: jdh, 전재 설명 하 세 요.
환경:
호스트: WIN 8
개발 환경: 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();
}

좋은 웹페이지 즐겨찾기