Приветствую.
Накидал вот такой код:
Идет вызов сначала StartRecord() затем StopRecord()
При использовании QDataStream Звук воспроизводится, но 1 из QString Теряется. Вот вывод в консоль:
default format not supported try to use nearest
"point 1"
""
StartPlay
default format not supported try to use nearest
Интересуют строки
QDataStream out(&file);
out << (QString)"point 1";
out << (QByteArray)buffer->buffer();
out << (QString)"point 2";
и
QString a;
in >> a;
qDebug() << a;
in >> ar;
in >> a;
qDebug() << a;
recorder.h
#ifndef RECORDER_H
#define RECORDER_H
#include <QObject>
#include "QDebug"
#include "QtMultimedia/QAudioFormat"
#include "QtMultimedia/QAudioDeviceInfo"
#include "QtMultimedia/QAudioInput"
#include "QtMultimedia/QAudioOutput"
#include "QBuffer"
#include "QFile"
class Recorder : public QObject
{
Q_OBJECT
public:
explicit Recorder(QObject *parent = 0);
signals:
public slots:
void StartRecord();
void StopRecord();
void StartPlay();
private:
QAudioInput * audio_input;
QAudioOutput * audio_out;
QBuffer * buffer;
QBuffer * tmp;
private slots:
};
#endif // RECORDER_H
recorder.cpp
#include "recorder.h"
Recorder::Recorder(QObject *parent) :
QObject(parent)
{
}
void Recorder::StartRecord(){
buffer = new QBuffer;
buffer->open(QIODevice::ReadWrite);
QAudioFormat format;
format.setFrequency(8000);
format.setChannels(1);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format))
{
qWarning() << "default format not supported try to use nearest";
format = info.nearestFormat(format);
}
audio_input = new QAudioInput(format,this);
audio_input->start(buffer);
}
void Recorder::StopRecord(){
audio_input->stop();
QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << (QString)"point 1";
out << (QByteArray)buffer->buffer();
out << (QString)"point 2";
StartPlay();
}
void Recorder::StartPlay(){
tmp = new QBuffer();
tmp->open(QIODevice::ReadWrite);
QFile file("file.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QByteArray ar;
QString a;
in >> a;
qDebug() << a;
in >> ar;
in >> a;
qDebug() << a;
tmp->write(ar);
tmp->seek(0);
qDebug() << "StartPlay";
QAudioFormat format;
format.setFrequency(8000);
format.setChannels(1);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format))
{
qWarning() << "default format not supported try to use nearest";
format = info.nearestFormat(format);
}
audio_out = new QAudioOutput(format,this);
audio_out->start(tmp);
}
Всем спасибо кто откликнулся.
Добавил QFile.Close() и все встало на свои места.