Files
qtmultimedia/doc/src/snippets/multimedia-snippets/audiorecorder.cpp
Jason McDonald 5e801e2793 Remove "All rights reserved" line from license headers.
As in the past, to avoid rewriting various autotests that contain
line-number information, an extra blank line has been inserted at the
end of the license text to ensure that this commit does not change the
total number of lines in the license header.

Change-Id: I20e5215108c6ebd5f8474fed5c3665118e4791e6
Reviewed-by: Rohan McGovern <rohan.mcgovern@nokia.com>
2012-01-30 07:08:05 +01:00

214 lines
6.3 KiB
C++

/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtWidgets>
#include <qaudiorecorder.h>
#include <qmediaservice.h>
#include <QtMultimedia/qaudioformat.h>
#include "audiorecorder.h"
AudioRecorder::AudioRecorder()
{
//! [create-objs-1]
capture = new QAudioRecorder();
//! [create-objs-1]
// set a default file
capture->setOutputLocation(QUrl("test.raw"));
QWidget *window = new QWidget;
QGridLayout* layout = new QGridLayout;
QLabel* deviceLabel = new QLabel;
deviceLabel->setText("Devices");
deviceBox = new QComboBox(this);
deviceBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
QLabel* codecLabel = new QLabel;
codecLabel->setText("Codecs");
codecsBox = new QComboBox(this);
codecsBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
QLabel* qualityLabel = new QLabel;
qualityLabel->setText("Quality");
qualityBox = new QComboBox(this);
qualityBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
//! [device-list]
for(int i = 0; i < audiosource->deviceCount(); i++)
deviceBox->addItem(audiosource->name(i));
//! [device-list]
//! [codec-list]
QStringList codecs = capture->supportedAudioCodecs();
for(int i = 0; i < codecs.count(); i++)
codecsBox->addItem(codecs.at(i));
//! [codec-list]
qualityBox->addItem("Low");
qualityBox->addItem("Medium");
qualityBox->addItem("High");
connect(capture, SIGNAL(durationChanged(qint64)), this, SLOT(updateProgress(qint64)));
connect(capture, SIGNAL(stateChanged(QMediaRecorder::State)), this, SLOT(stateChanged(QMediaRecorder::State)));
layout->addWidget(deviceLabel,0,0,Qt::AlignHCenter);
connect(deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int)));
layout->addWidget(deviceBox,0,1,1,3,Qt::AlignLeft);
layout->addWidget(codecLabel,1,0,Qt::AlignHCenter);
connect(codecsBox,SIGNAL(activated(int)),SLOT(codecChanged(int)));
layout->addWidget(codecsBox,1,1,Qt::AlignLeft);
layout->addWidget(qualityLabel,1,2,Qt::AlignHCenter);
connect(qualityBox,SIGNAL(activated(int)),SLOT(qualityChanged(int)));
layout->addWidget(qualityBox,1,3,Qt::AlignLeft);
fileButton = new QPushButton(this);
fileButton->setText(tr("Output File"));
connect(fileButton,SIGNAL(clicked()),SLOT(selectOutputFile()));
layout->addWidget(fileButton,3,0,Qt::AlignHCenter);
button = new QPushButton(this);
button->setText(tr("Record"));
connect(button,SIGNAL(clicked()),SLOT(toggleRecord()));
layout->addWidget(button,3,3,Qt::AlignHCenter);
recTime = new QLabel;
recTime->setText("0 sec");
layout->addWidget(recTime,4,0,Qt::AlignHCenter);
window->setLayout(layout);
setCentralWidget(window);
window->show();
active = false;
}
AudioRecorder::~AudioRecorder()
{
delete capture;
delete audiosource;
}
void AudioRecorder::updateProgress(qint64 pos)
{
currentTime = pos;
if(currentTime == 0) currentTime = 1;
QString text = QString("%1 secs").arg(currentTime/1000);
recTime->setText(text);
}
void AudioRecorder::stateChanged(QMediaRecorder::State state)
{
qWarning()<<"stateChanged() "<<state;
}
void AudioRecorder::deviceChanged(int idx)
{
//! [get-device]
for(int i = 0; i < audiosource->deviceCount(); i++) {
if(deviceBox->itemText(idx).compare(audiosource->name(i)) == 0)
audiosource->setSelectedDevice(i);
}
//! [get-device]
}
void AudioRecorder::codecChanged(int idx)
{
Q_UNUSED(idx);
//capture->setAudioCodec(codecsBox->itemText(idx));
}
void AudioRecorder::qualityChanged(int idx)
{
Q_UNUSED(idx);
/*
if(capture->audioCodec().compare("audio/pcm") == 0) {
if(qualityBox->itemText(idx).compare("Low") == 0) {
// 8000Hz mono is 8kbps
capture->setAudioBitrate(8);
} else if(qualityBox->itemText(idx).compare("Medium") == 0) {
// 22050Hz mono is 44.1kbps
capture->setAudioBitrate(44);
} else if(qualityBox->itemText(idx).compare("High") == 0) {
// 44100Hz mono is 88.2kbps
capture->setAudioBitrate(88);
}
}
*/
}
//! [toggle-record]
void AudioRecorder::toggleRecord()
{
if(!active) {
recTime->setText("0 sec");
currentTime = 0;
capture->record();
button->setText(tr("Stop"));
active = true;
} else {
capture->stop();
button->setText(tr("Record"));
active = false;
}
}
//! [toggle-record]
void AudioRecorder::selectOutputFile()
{
QStringList fileNames;
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
if (dialog.exec())
fileNames = dialog.selectedFiles();
if(fileNames.size() > 0)
capture->setOutputLocation(QUrl(fileNames.first()));
}