Advertisement
Guest User

mainwindow.cpp

a guest
Apr 3rd, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5.     QMainWindow(parent),
  6.     ui(new Ui::MainWindow),
  7.     m_audio(this),
  8.     m_playing(false)
  9. {
  10.     ui->setupUi(this);
  11.  
  12.     connect(&m_timer, SIGNAL(timeout()), this, SLOT(timer()));
  13.  
  14.     m_timer.start();
  15. }
  16.  
  17. MainWindow::~MainWindow()
  18. {
  19.     delete ui;
  20. }
  21.  
  22. #include <QAudioDeviceInfo>
  23. #include <QDebug>
  24.  
  25. // =====================================================================================================================
  26. AudioManager::AudioManager(QObject *parent) :
  27.     QObject(parent)
  28.   , m_audioOutputDevice(NULL)
  29.   , m_audioOutput(NULL)
  30. {
  31.     // PCM{U/A}
  32.     m_pcmFormat.setByteOrder(QAudioFormat::LittleEndian);
  33.     m_pcmFormat.setChannelCount(1);
  34.     m_pcmFormat.setCodec("audio/pcm");
  35.     m_pcmFormat.setFrequency(8000);
  36.     m_pcmFormat.setSampleSize(16);
  37.     m_pcmFormat.setSampleType(QAudioFormat::SignedInt);
  38. }
  39.  
  40. // =====================================================================================================================
  41. void AudioManager::start()
  42. {
  43.     m_audioOutput = new QAudioOutput(m_pcmFormat, this);
  44.     m_audioOutputDevice = m_audioOutput->start();
  45.     qDebug() << "Started output";
  46. }
  47. void AudioManager::stop()
  48. {
  49.     qDebug() << "Stopping output";
  50.     m_audioOutput->stop();
  51.  
  52.     qDebug() << "Stopped output";
  53.     m_audioOutputDevice = NULL;
  54.  
  55.     qDebug() << "Delete output";
  56.     delete m_audioOutput;
  57.     m_audioOutput = NULL;
  58.     qDebug() << "Output reset";
  59. }
  60.  
  61. // =====================================================================================================================
  62. void AudioManager::playAudio(QByteArray data)
  63. {
  64.     if (!m_audioOutputDevice || !m_audioOutput)
  65.     {
  66.         qDebug() << "Cannot play, device: " << m_audioOutputDevice << " output: " << m_audioOutput;
  67.         return;
  68.     }
  69.  
  70.     m_audioOutputDevice->write(data);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement