Russian Qt Forum
Сентябрь 30, 2024, 16:36 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: [1]   Вниз
  Печать  
Автор Тема: QDataStream теряются данные  (Прочитано 1580 раз)
destard
Гость
« : Январь 04, 2014, 12:51 »

Приветствую.
Накидал вот такой код:

Идет вызов сначала 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);
}
« Последнее редактирование: Январь 04, 2014, 12:56 от destard » Записан
destard
Гость
« Ответ #1 : Январь 04, 2014, 13:33 »

Всем спасибо кто откликнулся.
Добавил QFile.Close() и все встало на свои места.
Записан
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.045 секунд. Запросов: 20.