I have a simple AudioRecorder in my QML-application. As there is no QML-control directly available for recording Audio with Qt 5, I made an own class derieved from QAudioRecorder :
#include
#include
#include
#include
#include
#include
class AudioRecorder : public QAudioRecorder
{
Q_OBJECT
QML_ELEMENT
public:
AudioRecorder(QObject* parent = Q_NULLPTR) : QAudioRecorder(parent)
{
QString d = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::MusicLocation);
if (!d.isEmpty())
setOutputLocation(QUrl::fromLocalFile(d));
}
};
In my app I have a simple button which is being used for starting and stopping the recording:
// My own QML element
AudioRecorder {
id: audioRecorder
}
Button {
id: recButton
onClicked: {
if (audioRecorder.state === MediaState.StoppedState)
audioRecorder.record()
else if (audioRecorder.state === MediaState.RecordingState)
audioRecorder.stop()
}
}
After recording, there is another button appearing which can be used to remove the created audio-file. I remove the file with a helper-class which provides some functions I cannot access from QML directly:
Button {
id: remButton
visible: audioRecorder.state === MediaState.StoppedState
onClicked: QMLHelper.removeFile(audioRecorder.actualLocation)
}
class QMLHelper : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
public:
QMLHelper(QObject* parent = Q_NULLPTR) : QObject(parent) { Q_UNUSED(parent) };
Q_INVOKABLE bool removeFile(const QString& path)
{
if (!path.isEmpty())
{
QFile f(path);
bool r = f.remove();
qDebug() << r << ": " << r.errorString();
return r;
}
return false;
};
}
The output when I try to remove the file is:
false: The process cannot access the file because it is being used by another process.
Why is the QAudioRecorder not releasing the file after calling stop() ? How can I release the file to remove it?
Remark: I tried it with an QML videoRecorder, and here it does work without problems:
Camera {
id: camera
videoRecorder {
id: vr
}
}
Button {
id: vidButton
visible: camera.videoRecorder.recorderStatus !== CameraRecorder.UnavailableStatus
onClicked: {
if (camera.videoRecorder.recorderState === CameraRecorder.StoppedState)
camera.videoRecorder.record()
else if (camera.videoRecorder.recorderState === CameraRecorder.RecordingState)
camera.videoRecorder.stop()
}
Button {
id: vidRemButton
visible: camera.videoRecorder.recorderState === CameraRecorder.StoppedState
onClicked: QMLHelper.removeFile(camera.videoRecorder.actualLocation)
}
Output:
true