• 大小: 5KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-22
  • 语言: 其他
  • 标签: QT  录音  C++  

资源简介

用QT写的简单的录音例子,简单易学,可以作为参考.

资源截图

代码片段和文件信息

#include “audiorecorder.h“
#include 

struct HEADER
{
 char RIFFNAME[4];
 unsigned long nRIFFLength;
 char WAVNAME[4];
 char FMTNAME[4];
 unsigned long nFMTLength;
 unsigned short nAudioFormat;
 unsigned short nChannleNumber;
 //unsigned short nSampleRate;
 unsigned long nSampleRate;
 unsigned long nBytesPerSecond;
 unsigned short nBytesPerSample;
 unsigned short    nBitsPerSample;
 char    DATANAME[4];
 unsigned long   nDataLength;
};

AudioRecorder::AudioRecorder(QWidget *parent)
    : QWidget(parent)
{
    start = new QPushButton(tr(“start“));
    stop = new QPushButton(tr(“stop“));
    play = new QPushButton(tr(“play“));
    save = new QPushButton(tr(“save“));
    stop->setDisabled(true);
    play->setDisabled(true);
    save->setDisabled(true);

    debugInfo = new QTextEdit();

    QHBoxLayout *hlayout = new QHBoxLayout();
    hlayout->addWidget(start);
    hlayout->addWidget(stop);
    hlayout->addWidget(play);
    hlayout->addWidget(save);

   // QHBoxLayout *hlayout1 = new QHBoxLayout();


    QVBoxLayout *layout = new QVBoxLayout();
    layout->addLayout(hlayout);
    layout->addWidget(debugInfo);

    this->setLayout(layout);

    outputFile = new QFile();
    outputFile->setFileName(tr(“record.raw“));

    format.setFrequency(8000);
    format.setChannels(1);
    format.setSampleSize(16);
    format.setSampleType(QAudioFormat::SignedInt);
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setCodec(“audio/pcm“);

    QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
    if (!info.isFormatSupported(format)) {
        qWarning(“input default format not supported try to use nearest“);
        format = info.nearestFormat(format);
    }



    QAudioDeviceInfo info1(QAudioDeviceInfo::defaultOutputDevice());
    if (!info1.isFormatSupported(format)) {
           qWarning() << “output default format not supported - trying to use nearest“;
//           format = info.nearestFormat(m_format);
           qWarning() << “output no support input format.“;
           return;
    }

    if(format.sampleSize() != 16) {
        qWarning(“audio device doesn‘t support 16 bit support %d bit samples example cannot run“ format.sampleSize());
        audioInput = 0;
        return;
    }

    audioInput = NULL;
    audioOutput = NULL;

    connect(start SIGNAL(clicked()) thisSLOT(recordStart()));
    connect(stop SIGNAL(clicked()) thisSLOT(recordStop()));
    connect(play SIGNAL(clicked()) thisSLOT(recordPlay()));
    connect(save SIGNAL(clicked()) thisSLOT(recordSave()));
}

AudioRecorder::~AudioRecorder()
{

}

void AudioRecorder::recordStart()
{
    outputFile->open(QIODevice::WriteOnly | QIODevice::Truncate);
    audioInput = new QAudioInput(format this);
    audioInput->start(outputFile);
    debugInfo->append(tr(“start record!“));    
    start->setDisabled(true);
    stop->setDisabled(false);
    play-

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6998  2010-08-26 11:49  audioRecorder\audiorecorder.cpp

     文件        818  2010-08-26 11:30  audioRecorder\audiorecorder.h

     文件        578  2010-08-26 17:08  audioRecorder\audioRecorder.pro

     文件      14336  2014-07-05 09:51  audioRecorder\audioRecorder.pro.user

     文件        255  2010-08-24 15:41  audioRecorder\main.cpp

     目录          0  2014-07-05 09:52  audioRecorder

----------- ---------  ---------- -----  ----

                22985                    6


评论

共有 条评论