i am writing the application to send voice by lan network using...
i am having trouble because code doesn't work
below is the class ThreadInputAudio using thread to constantly send my voice
threadinputaudio.h
#ifndef THREADINPUTAUDIO_H
#define THREADINPUTAUDIO_H
#include <QThread>
#include <QByteArray>
#include <QAudioInput>
#include <QTcpSocket>
#include <QVariant>
class ThreadInputAudio
: public QThread{
Q_OBJECT
public:
ThreadInputAudio();
void stop();
protected:
void run();
private:
bool stopped;
QAudioInput *m_audioInput;
QAudioFormat m_format;
void stopRecording();
private slots:
void readMore();
};
#endif // THREADINPUTAUDIO_H
#ifndef THREADINPUTAUDIO_H
#define THREADINPUTAUDIO_H
#include <QThread>
#include <QByteArray>
#include <QAudioInput>
#include <QTcpSocket>
#include <QVariant>
class ThreadInputAudio : public QThread
{
Q_OBJECT
public:
ThreadInputAudio();
void stop();
QTcpSocket* socket;
protected:
void run();
private:
bool stopped;
QByteArray m_buffer;
QAudioInput *m_audioInput;
QAudioFormat m_format;
QIODevice *m_input;
void stopRecording();
private slots:
void readMore();
};
#endif // THREADINPUTAUDIO_H
To copy to clipboard, switch view to plain text mode
threadinputaudio.cpp
#include "threadinputaudio.h"
ThreadInputAudio::ThreadInputAudio()
:m_audioInput(0)
,m_input(0)
{
stopped = false;
}
void ThreadInputAudio::run(){
m_format.setFrequency(8000);
m_format.setChannels(1);
m_format.setSampleSize(16);
m_format.setSampleType(QAudioFormat::SignedInt);
m_format.setByteOrder(QAudioFormat::LittleEndian);
m_format.setCodec("audio/pcm");
QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
if (!info.isFormatSupported(m_format)) {
qDebug() << "Default format not supported - trying to use nearest";
m_format = info.nearestFormat(m_format);
}
while (!stopped){
m_audioInput = new QAudioInput(m_format);
m_input = m_audioInput->start();
connect(m_input, SIGNAL(readyRead()), SLOT(readMore()));
stopRecording();
}
stopped = false;
}
void ThreadInputAudio::stop(){
stopped = true;
}
void ThreadInputAudio::stopRecording(){
m_audioInput->stop();
delete m_audioInput;
}
void ThreadInputAudio::readMore(){
qDebug() << "start send voice";
if(!m_audioInput)
return;
qint64 len = m_audioInput->bytesReady();
qint64 l = m_input->read(m_buffer.data(), len);
if(l > 0) {
if(socket == NULL) return;
out << (quint32) 0;
out << msg;
out << data;
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
socket->write(block);
socket->flush();
qDebug() << "send voice ok";
}
}
#include "threadinputaudio.h"
ThreadInputAudio::ThreadInputAudio()
:m_audioInput(0)
,m_input(0)
{
stopped = false;
}
void ThreadInputAudio::run(){
m_format.setFrequency(8000);
m_format.setChannels(1);
m_format.setSampleSize(16);
m_format.setSampleType(QAudioFormat::SignedInt);
m_format.setByteOrder(QAudioFormat::LittleEndian);
m_format.setCodec("audio/pcm");
QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
if (!info.isFormatSupported(m_format)) {
qDebug() << "Default format not supported - trying to use nearest";
m_format = info.nearestFormat(m_format);
}
while (!stopped){
m_audioInput = new QAudioInput(m_format);
m_input = m_audioInput->start();
connect(m_input, SIGNAL(readyRead()), SLOT(readMore()));
QThread::sleep(500);
stopRecording();
}
stopped = false;
}
void ThreadInputAudio::stop(){
stopped = true;
}
void ThreadInputAudio::stopRecording(){
m_audioInput->stop();
delete m_audioInput;
}
void ThreadInputAudio::readMore(){
qDebug() << "start send voice";
if(!m_audioInput)
return;
qint64 len = m_audioInput->bytesReady();
qint64 l = m_input->read(m_buffer.data(), len);
if(l > 0) {
if(socket == NULL) return;
if(socket->state() != QAbstractSocket::ConnectedState ) return;
QString msg = "VOICE";
QVariant data = QVariant(m_buffer);
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_2);
out << (quint32) 0;
out << msg;
out << data;
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
socket->write(block);
socket->flush();
qDebug() << "send voice ok";
}
}
To copy to clipboard, switch view to plain text mode
i can't figure out why the method readmore() is never done
help me to decide whether the code on receiving the data is correct or not? how can i fix it.
void MainWindow
::onVoiceMessage(const QByteArray &data
){ qDebug() << "nhan duoc am thanh";
QAudioFormat format;
inputFile->write(data.constData(),(qint64)data.size());
format.setFrequency(8000);
format.setChannels(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format)) {
qDebug() <<"raw audio format not supported by backend, cannot play audio.";
return;
}
audio = new QAudioOutput(format);
connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
audio->start(inputFile);
}
void MainWindow::finishedPlaying(QAudio::State state)
{
if(state == QAudio::IdleState) {
audio->stop();
delete audio;
}
}
void MainWindow::onVoiceMessage(const QByteArray &data){
qDebug() << "nhan duoc am thanh";
QAudioFormat format;
inputFile->write(data.constData(),(qint64)data.size());
format.setFrequency(8000);
format.setChannels(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format)) {
qDebug() <<"raw audio format not supported by backend, cannot play audio.";
return;
}
audio = new QAudioOutput(format);
connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
audio->start(inputFile);
}
void MainWindow::finishedPlaying(QAudio::State state)
{
if(state == QAudio::IdleState) {
audio->stop();
delete audio;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks