In the QML API, there is currently no corresponding API for recording. We must use the QT C + + API Qaudiorecorder to do the recording work. In this article, we'll show you how to use this API for recording.
First, let's create an app with the "QML app with C + + plugin (qmake)" template. Note that the Qmake project must be on target 15.04 and above to run.
For recording, I created a class called "Audiorecorder":
Audiorecorder.h
#ifndef audiorecorder_h#define audiorecorder_h#include <QAudioRecorder> #include <qurl>class Audiorecorder:public qobject{q_object Q_property (bool recording READ recording NOTIFY recordingchanged) Q_P Roperty (QString name READ name WRITE setName NOTIFY namechanged) public:explicit audiorecorder (Qobject *parent = 0); const BOOL Recording () const; QString name () const; Q_invokable qstringlist supportedaudiocodecs (); Q_invokable qstringlist supportedcontainers (); Q_invokable Qurl Path () {return m_path; }signals:void recordingchanged (BOOL); void Namechanged (Qurl);p ublic slots:void setName (QString name); void Setrecording (bool recording); void record (); void Stop ();p rivate:qstring getfilepath (const QString filename) const;private:qaudiorecorder * m_audiorecorder; BOOL m_recording; QString M_name; Qurl M_path;}; #endif//Audiorecorder_h
Audiorecorder.cpp
#include <QUrl> #include <QStandardPaths> #include <QDir> #include "audiorecorder.h" Audiorecorder:: Audiorecorder (Qobject *parent): Qobject (parent) {M_audiorecorder = new Qaudiorecorder (this); Qaudioencodersettings audiosettings; Audiosettings.setcodec ("AUDIO/PCM"); Audiosettings.setquality (qmultimedia::highquality); M_audiorecorder->setencodingsettings (audiosettings); HTTPS://FORUM.QT.IO/TOPIC/42541/RECORDING-AUDIO-USING-QTAUDIORECORDER/2 m_audiorecorder-> Setcontainerformat ("wav"); M_recording = false;} const BOOL Audiorecorder::recording () const{return m_recording;} void Audiorecorder::setrecording (bool recording) {if (m_recording = = recording) return; M_recording = recording; Emit recordingchanged (m_recording);} void Audiorecorder::record () {qdebug () << "Entering record!"; if (m_audiorecorder->state () = = Qmediarecorder::stoppedstate) {qdebug () << "recording....!"; M_audiorecorDer->record (); M_recording = true; Qdebug () << "m_recording:" << m_recording; Emit recordingchanged (m_recording); }}void Audiorecorder::stop () {qdebug () << "Entering stop!"; if (m_audiorecorder->state () = = Qmediarecorder::recordingstate) {qdebug () << "stopping ..."; M_audiorecorder->stop (); M_recording = false; Emit recordingchanged (m_recording); }}qstring audiorecorder::name () const{return m_name;} void Audiorecorder::setname (QString name) {if (m_name = = name) return; M_name = name; Emit namechanged (name); At the same time update the path M_path = Qurl (GetFilePath (name)); Set the path m_audiorecorder->setoutputlocation (M_path);} Qstringlist Audiorecorder::supportedaudiocodecs () {return m_audiorecorder->supportedaudiocodecs ();} Qstringlist audiorecorder::supportedcontainers () {return m_audiorecorder->supportedcontainers ();} QString AudioRecorder::getfilepath (const QString filename) const{QString writablepath = qstandardpaths:: Writablelocatio N (qstandardpaths::D atalocation); Qdebug () << "Writablepath:" << writablepath; QString Absolutepath = Qdir (Writablepath). Absolutepath (); Qdebug () << "Absoluepath:" << absolutepath; We need to make sure we had the path for storage Qdir dir (absolutepath); if (Dir.mkdir (Absolutepath)) {qdebug () << "successfully created the path!"; } QString Path = Absolutepath + "/" + filename; Qdebug () << "path:" << path; return path;}
Here we use Qstandardpath to get a directory of files that can be accessed on Ubuntu phones. The use of this Qaudiorecorder API is also very straightforward.
The interface of our MAIN.QML is also very simple:
Main.qml
Import QtQuick 2.0import ubuntu.components 1.1import qtmultimedia 5.0import audiorecorder 1.0/*! \brief MainView with a Label and Button Elements.*/mainview {//ObjectName for functional testing purposes (autopilot- QT5) ObjectName: "MainView"//note! ApplicationName needs to match the "name" field of the click Manifest ApplicationName: "Audiorecorder.liu-xiao-guo" /* The enables the application to change orientation when the device is rotated. The default is False. *///automaticorientation:true//Removes the old toolbar and enables new features of the new header. Usedeprecatedtoolbar:false width:units.gu (height:units.gu) page {title:i18n.tr ("Audiorecorder") ) Audiorecorder {id:audio name: "Sample.wav" onrecordingchanged: { Console.log ("Recording:" + recording); }} MediaPlayer {Id:player autoplay:true volume:1.0} Column {anchors.fill:parent spacing:units.gu (1) Label {text: "Supported Audio Codecs:"} ListView {Id:audiocodecs Width:parent.width Height:audiocodecs.contentHeight Model:audio.supportedAu Diocodecs () Delegate:text {text:modeldata}} Re Ctangle {width:parent.width height:units.gu (0.1)} Label { Text: "Supported Containers:"} ListView {Id:audiocontainer W Idth:parent.width height:audiocontainer.contentHeight model:audio.supportedContainers () Delegate:text {Text:modeldata}}} Row { Anchors.bottom:paRent.bottom Anchors.bottomMargin:units.gu (2) Anchors.horizontalCenter:parent.horizontalCenter Anchors.margins:units.gu (2) Spacing:units.gu (2) Button {Id:record Text: "Record Audio" enabled:!audio.recording onclicked: {Audio. Record (); }} button {Id:stop text: "Stop" onclicked: { Audio.stop (); Player.stop (); }} button {Id:play text: "Play Audio" onclicked: { Console.log ("Path:" + Audio.path ()); Player.source = Audio.path (); Player.play (); } } } }}
In QML, we use directly:
Audiorecorder { id:audio name: "Sample.wav" onrecordingchanged: { console.log ("Recording:" + recording); } }
We can use the "Record Audio" button to record the work. The application interface is as follows:
After the experiment on the mobile phone, the recording effect is very good, very clear. We can use the "Play Audio" button to play.
The source of the entire project is: Git clone https://gitcafe.com/ubuntu/audiorecorder.git
How to make a language recording in Ubuntu qml app