One dedicated file per voice note recording

This commit is contained in:
Sebastian Wolf 2021-01-04 22:03:04 +01:00
parent 81ffb53062
commit e0b94a0487
No known key found for this signature in database
GPG key ID: CEA9522B5F38A90A
3 changed files with 23 additions and 7 deletions

View file

@ -151,6 +151,9 @@ Item {
anchors.fill: parent anchors.fill: parent
onClicked: { onClicked: {
recordButton.visible = false; recordButton.visible = false;
recordingDone = false;
recordingDuration = 0;
handleRecordingDuration();
fernschreiberUtils.startRecordingVoiceNote(); fernschreiberUtils.startRecordingVoiceNote();
} }
} }

View file

@ -23,8 +23,10 @@
#include <QAudioEncoderSettings> #include <QAudioEncoderSettings>
#include <QStandardPaths> #include <QStandardPaths>
#include <QDir> #include <QDir>
#include <QDirIterator>
#include <QFile> #include <QFile>
#include <QUrl> #include <QUrl>
#include <QDateTime>
#include <QGeoCoordinate> #include <QGeoCoordinate>
#include <QGeoLocation> #include <QGeoLocation>
@ -35,7 +37,7 @@ FernschreiberUtils::FernschreiberUtils(QObject *parent) : QObject(parent)
{ {
LOG("Initializing audio recorder..."); LOG("Initializing audio recorder...");
QString temporaryDirectoryPath = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + + "/harbour-fernschreiber"; QString temporaryDirectoryPath = this->getTemporaryDirectoryPath();
QDir temporaryDirectory(temporaryDirectoryPath); QDir temporaryDirectory(temporaryDirectoryPath);
if (!temporaryDirectory.exists()) { if (!temporaryDirectory.exists()) {
temporaryDirectory.mkpath(temporaryDirectoryPath); temporaryDirectory.mkpath(temporaryDirectoryPath);
@ -47,7 +49,6 @@ FernschreiberUtils::FernschreiberUtils(QObject *parent) : QObject(parent)
encoderSettings.setQuality(QMultimedia::LowQuality); encoderSettings.setQuality(QMultimedia::LowQuality);
this->audioRecorder.setEncodingSettings(encoderSettings); this->audioRecorder.setEncodingSettings(encoderSettings);
this->audioRecorder.setContainerFormat("ogg"); this->audioRecorder.setContainerFormat("ogg");
this->audioRecorder.setOutputLocation(QUrl::fromLocalFile(temporaryDirectoryPath + "/voicenote.ogg"));
QMediaRecorder::Status audioRecorderStatus = this->audioRecorder.status(); QMediaRecorder::Status audioRecorderStatus = this->audioRecorder.status();
this->handleAudioRecorderStatusChanged(audioRecorderStatus); this->handleAudioRecorderStatusChanged(audioRecorderStatus);
@ -194,7 +195,8 @@ QString FernschreiberUtils::getUserName(const QVariantMap &userInformation)
void FernschreiberUtils::startRecordingVoiceNote() void FernschreiberUtils::startRecordingVoiceNote()
{ {
LOG("Start recording voice note..."); LOG("Start recording voice note...");
this->cleanUp(); QDateTime thisIsNow = QDateTime::currentDateTime();
this->audioRecorder.setOutputLocation(QUrl::fromLocalFile(this->getTemporaryDirectoryPath() + "/voicenote-" + thisIsNow.toString("yyyy-MM-dd-HH-mm-ss") + ".ogg"));
this->audioRecorder.setVolume(1); this->audioRecorder.setVolume(1);
this->audioRecorder.record(); this->audioRecorder.record();
} }
@ -287,9 +289,19 @@ void FernschreiberUtils::cleanUp()
if (this->geoPositionInfoSource) { if (this->geoPositionInfoSource) {
this->geoPositionInfoSource->stopUpdates(); this->geoPositionInfoSource->stopUpdates();
} }
QString voiceNotePath = this->voiceNotePath(); QString temporaryDirectoryPath = this->getTemporaryDirectoryPath();
if (QFile::exists(voiceNotePath)) { QDirIterator temporaryDirectoryIterator(temporaryDirectoryPath, QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks, QDirIterator::Subdirectories);
LOG("Removing old temporary file..."); while (temporaryDirectoryIterator.hasNext()) {
QFile::remove(voiceNotePath); QString nextFilePath = temporaryDirectoryIterator.next();
if (QFile::remove(nextFilePath)) {
LOG("Temporary file removed " << nextFilePath);
} else {
LOG("Error removing temporary file " << nextFilePath);
} }
} }
}
QString FernschreiberUtils::getTemporaryDirectoryPath()
{
return QStandardPaths::writableLocation(QStandardPaths::TempLocation) + + "/harbour-fernschreiber";
}

View file

@ -69,6 +69,7 @@ private:
QGeoPositionInfoSource *geoPositionInfoSource; QGeoPositionInfoSource *geoPositionInfoSource;
void cleanUp(); void cleanUp();
QString getTemporaryDirectoryPath();
}; };