PDA

View Full Version : How to record and play an audio file in qml?



harish
8th February 2012, 09:19
Hi all, I am trying an app to record and play an audio file using QML.

I had downloaded this API from this link:https://projects.developer.nokia.com/audiorecorder/changeset/67519324bc6ea96ef956dec1b494eb3a5417402b and tried but i was not able to get the required output.I was not able to run the example project from the given link.

I am having an issue in solving this.

Actually what i am trying is:I am having 3 buttons in the mainPage as RECORD,STOP and PLAY.

When i click on the RECORD button the voice which i speak should be recorded and when i click on the STOP button the operation should stop and recorded voice should be stored.Finally when i click on PLAY button i should be able to get the recorded voice and play it.

Can anyone help me this app so that i will able to solve my issue….

Thanks in advance,
Harish

Edit / Delete Edit Post Quick reply to this message Reply Reply With Quote Reply With Quote Multi-Quote This Message

harish
9th February 2012, 11:22
Hi everyone, I have tried to record and play an audio file in QML but i was not able to fix my errors.

Here is the code what i had tried so far:

main.qml: import QtQuick 1.0
import com.nokia.symbian 1.0
import Qt 4.7

Page {
id: mainPage


Rectangle {
id: outerRect
width: 360
height: 620


Button{
id: buttonRecord
x: 0
y: 368
width: outerRect.width/3
text: "Record"
anchors.bottomMargin: 210
anchors.leftMargin: 0
anchors.bottom: outerRect.bottom; anchors.left: outerRect.left
MouseArea {
id: mouseArea4
x: 0
y: 0
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.topMargin: 0
anchors.fill: parent

onClicked: {
StringHelper.record()

}
}
}


Button{
id: buttonStop
x: 120
y: 368
width: outerRect.width/3
text: "Stop"
anchors.bottomMargin: 210
anchors.leftMargin: 0
anchors.bottom: outerRect.bottom; anchors.left: buttonRecord.right

MouseArea {
id: mouseArea3
anchors.fill: parent
onClicked: {
StringHelper.stop()


}
}
}


Button{
id: buttonPlay
x: 240
y: 368
width: outerRect.width/3
text: "Play"
anchors.bottomMargin: 210
anchors.leftMargin: 0
anchors.bottom: outerRect.bottom; anchors.left: buttonStop.right

MouseArea {
id: mouseArea2
anchors.fill: parent

onClicked: {
StringHelper.play()

}
}
}


TextInput {
id: textDisplay
x: 21
y: 49
width: outerRect.width - buttonExit.width
height: buttonExit.height
text: "Audio Recorder "
anchors.leftMargin: 21
anchors.topMargin: 49
readOnly: true
font.bold: true
horizontalAlignment: TextInput.AlignHCenter
font.pixelSize: 25
anchors.top: outerRect.top
anchors.left: outerRect.left
MouseArea {
anchors.fill: parent
onClicked: textDisplay.showText()
}
}


Button{
id: buttonExit
text: "Exit"
anchors.top: outerRect.top
anchors.right: outerRect.right

MouseArea {id: mouseArea6;
anchors.fill: parent
onClicked: {Qt.quit();
}
}
}
}
}

Stringhelper.cpp:

#include"stringhelper.h"
#include<QMessageBox>

#include"QAudioCaptureSource"
#include<QMediaPlayer>


void StringHelper::record()
{

QAudioCaptureSource *audiosource = new QAudioCaptureSource;
QMediaRecorder *recorder = new QMediaRecorder(audiosource);

QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/vorbis");
audioSettings.setQuality(QtMultimediaKit::HighQual ity);

recorder->setEncodingSettings(audioSettings);
recorder->setOutputLocation(QUrl::fromLocalFile("C:\Kalimba.mp3"));
recorder->record();



}

void StringHelper::stop()
{

QAudioCaptureSource *audiosource = new QAudioCaptureSource;
QMediaRecorder *recorder = new QMediaRecorder(audiosource);

QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/vorbis");
audioSettings.setQuality(QtMultimediaKit::HighQual ity);

recorder->setEncodingSettings(audioSettings);
recorder->setOutputLocation(QUrl::fromLocalFile("C:\Kalimba.mp3"));
recorder->stop();


}


void StringHelper::play()
{
QMediaPlayer *player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile("C:\Kalimba.mp3"));
player->setVolume(50);
player->play();
}

I am getting the following errors:“DirectShowPlayerService::doSetUrlSou rce: Unresolved error code 80070020
QFSFileEngine::open: No file name specified
QFile::seek: IODevice is not open”

Can anyone help me with this?

Harish.M

Le_B
17th February 2012, 14:42
i may say something stupod but you need to double the backslashes in c++ strings
player->setMedia(QUrl::fromLocalFile("C:\Kalimba.mp3")) ;
or just use slashes to avoid this problem