Update audiooutput example with volume slider
authorhawcroft <derick.hawcroft@nokia.com>
Wed, 28 Sep 2011 03:04:18 +0000 (13:04 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 28 Sep 2011 03:34:16 +0000 (05:34 +0200)
Update example for new volume API

Change-Id: I1b9ccbccc62930549e667f0063b3d76feb23a2ea
Reviewed-on: http://codereview.qt-project.org/5662
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: derick hawcroft <derick.hawcroft@nokia.com>
examples/audiooutput/audiooutput.cpp
examples/audiooutput/audiooutput.h

index 68fbbcc..b8193fb 100644 (file)
@@ -51,6 +51,7 @@ const QString AudioTest::PushModeLabel(tr("Enable push mode"));
 const QString AudioTest::PullModeLabel(tr("Enable pull mode"));
 const QString AudioTest::SuspendLabel(tr("Suspend playback"));
 const QString AudioTest::ResumeLabel(tr("Resume playback"));
+const QString AudioTest::VolumeLabel(tr("Volume:"));
 
 const int DurationSeconds = 1;
 const int ToneFrequencyHz = 600;
@@ -190,6 +191,18 @@ void AudioTest::initializeWindow()
     connect(m_suspendResumeButton, SIGNAL(clicked()), SLOT(toggleSuspendResume()));
     layout->addWidget(m_suspendResumeButton);
 
+    QHBoxLayout *volumeBox = new QHBoxLayout;
+    m_volumeLabel = new QLabel;
+    m_volumeLabel->setText(VolumeLabel);
+    m_volumeSlider = new QSlider(Qt::Horizontal);
+    m_volumeSlider->setMinimum(0);
+    m_volumeSlider->setMaximum(100);
+    m_volumeSlider->setSingleStep(10);
+    connect(m_volumeSlider, SIGNAL(valueChanged(int)), this, SLOT(volumeChanged(int)));
+    volumeBox->addWidget(m_volumeLabel);
+    volumeBox->addWidget(m_volumeSlider);
+    layout->addLayout(volumeBox);
+
     window->setLayout(layout.data());
     layout.take(); // ownership transferred
 
@@ -231,6 +244,7 @@ void AudioTest::createAudioOutput()
     connect(m_audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(stateChanged(QAudio::State)));
     m_generator->start();
     m_audioOutput->start(m_generator);
+    m_volumeSlider->setValue(int(m_audioOutput->volume()*100.0f));
 }
 
 AudioTest::~AudioTest()
@@ -248,6 +262,12 @@ void AudioTest::deviceChanged(int index)
     createAudioOutput();
 }
 
+void AudioTest::volumeChanged(int value)
+{
+    if (m_audioOutput)
+        m_audioOutput->setVolume(qreal(value/100.0f));
+}
+
 void AudioTest::notified()
 {
     qWarning() << "bytesFree = " << m_audioOutput->bytesFree()
index 519fa0c..3a07039 100644 (file)
@@ -45,6 +45,8 @@
 
 #include <QObject>
 #include <QMainWindow>
+#include <QLabel>
+#include <QSlider>
 #include <QIODevice>
 #include <QTimer>
 #include <QPushButton>
@@ -94,6 +96,8 @@ private:
     QPushButton*     m_modeButton;
     QPushButton*     m_suspendResumeButton;
     QComboBox*       m_deviceBox;
+    QLabel*          m_volumeLabel;
+    QSlider*         m_volumeSlider;
 
     QAudioDeviceInfo m_device;
     Generator*       m_generator;
@@ -108,6 +112,7 @@ private:
     static const QString PullModeLabel;
     static const QString SuspendLabel;
     static const QString ResumeLabel;
+    static const QString VolumeLabel;
 
 private slots:
     void notified();
@@ -116,6 +121,7 @@ private slots:
     void toggleSuspendResume();
     void stateChanged(QAudio::State state);
     void deviceChanged(int index);
+    void volumeChanged(int);
 };
 
 #endif