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
Qt Code:
  1. #ifndef THREADINPUTAUDIO_H
  2. #define THREADINPUTAUDIO_H
  3.  
  4. #include <QThread>
  5. #include <QByteArray>
  6. #include <QAudioInput>
  7. #include <QTcpSocket>
  8. #include <QVariant>
  9.  
  10. class ThreadInputAudio : public QThread
  11. {
  12. Q_OBJECT
  13. public:
  14. ThreadInputAudio();
  15. void stop();
  16. QTcpSocket* socket;
  17. protected:
  18. void run();
  19. private:
  20. bool stopped;
  21. QByteArray m_buffer;
  22. QAudioInput *m_audioInput;
  23. QAudioFormat m_format;
  24. QIODevice *m_input;
  25. void stopRecording();
  26.  
  27. private slots:
  28.  
  29. void readMore();
  30. };
  31.  
  32. #endif // THREADINPUTAUDIO_H
To copy to clipboard, switch view to plain text mode 

threadinputaudio.cpp
Qt Code:
  1. #include "threadinputaudio.h"
  2.  
  3. ThreadInputAudio::ThreadInputAudio()
  4. :m_audioInput(0)
  5. ,m_input(0)
  6. {
  7. stopped = false;
  8.  
  9. }
  10. void ThreadInputAudio::run(){
  11. m_format.setFrequency(8000);
  12. m_format.setChannels(1);
  13. m_format.setSampleSize(16);
  14. m_format.setSampleType(QAudioFormat::SignedInt);
  15. m_format.setByteOrder(QAudioFormat::LittleEndian);
  16. m_format.setCodec("audio/pcm");
  17. QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
  18. if (!info.isFormatSupported(m_format)) {
  19. qDebug() << "Default format not supported - trying to use nearest";
  20. m_format = info.nearestFormat(m_format);
  21. }
  22. while (!stopped){
  23. m_audioInput = new QAudioInput(m_format);
  24. m_input = m_audioInput->start();
  25. connect(m_input, SIGNAL(readyRead()), SLOT(readMore()));
  26. QThread::sleep(500);
  27. stopRecording();
  28. }
  29. stopped = false;
  30. }
  31. void ThreadInputAudio::stop(){
  32. stopped = true;
  33. }
  34. void ThreadInputAudio::stopRecording(){
  35. m_audioInput->stop();
  36. delete m_audioInput;
  37. }
  38. void ThreadInputAudio::readMore(){
  39. qDebug() << "start send voice";
  40. if(!m_audioInput)
  41. return;
  42. qint64 len = m_audioInput->bytesReady();
  43. qint64 l = m_input->read(m_buffer.data(), len);
  44. if(l > 0) {
  45. if(socket == NULL) return;
  46. if(socket->state() != QAbstractSocket::ConnectedState ) return;
  47. QString msg = "VOICE";
  48. QVariant data = QVariant(m_buffer);
  49. QByteArray block;
  50. QDataStream out(&block, QIODevice::WriteOnly);
  51. out.setVersion(QDataStream::Qt_4_2);
  52. out << (quint32) 0;
  53. out << msg;
  54. out << data;
  55. out.device()->seek(0);
  56. out << (quint32)(block.size() - sizeof(quint32));
  57.  
  58. socket->write(block);
  59. socket->flush();
  60.  
  61. qDebug() << "send voice ok";
  62. }
  63. }
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.
Qt Code:
  1. void MainWindow::onVoiceMessage(const QByteArray &data){
  2. qDebug() << "nhan duoc am thanh";
  3. QAudioFormat format;
  4. inputFile->write(data.constData(),(qint64)data.size());
  5. format.setFrequency(8000);
  6. format.setChannels(1);
  7. format.setSampleSize(8);
  8. format.setCodec("audio/pcm");
  9. format.setByteOrder(QAudioFormat::LittleEndian);
  10. format.setSampleType(QAudioFormat::UnSignedInt);
  11.  
  12. QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
  13. if (!info.isFormatSupported(format)) {
  14. qDebug() <<"raw audio format not supported by backend, cannot play audio.";
  15. return;
  16. }
  17.  
  18. audio = new QAudioOutput(format);
  19. connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
  20. audio->start(inputFile);
  21. }
  22.  
  23. void MainWindow::finishedPlaying(QAudio::State state)
  24. {
  25. if(state == QAudio::IdleState) {
  26. audio->stop();
  27. delete audio;
  28. }
  29. }
To copy to clipboard, switch view to plain text mode