Unsolved Recording audio works on windows but not on android
-
Hey guys, i want to record and save audio in a file. Here is the code is use:
audioRecorder.hprivate: QDir folder;
audioRecorder.cpp
QAudioEncoderSettings audioSettings; audioSettings.setCodec("audio/pcm"); audioSettings.setQuality(QMultimedia::HighQuality); audioRecorder.setEncodingSettings(audioSettings); folder = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); QString qdir = folder.path(); qdir.append(QString("/test.wav")); audioRecorder.setOutputLocation(QUrl::fromLocalFile(qdir)); void AudioRecorder::start() { audioRecorder.record(); } void AudioRecorder::stop() { audioRecorder.stop(); } void AudioRecorder::play() { QString qdir = folder.path(); qdir.append("/test.wav"); mediaPlayer.setMedia(QUrl::fromLocalFile(qdir)); mediaPlayer.setVolume(20); mediaPlayer.play(); }
Everything works as intended on windows . On android it looks like the mendiaRecorder is not able to create the file. I tried to open the file, but it doesnst exit after calling audioRecorder.record() and audioRecorder.stop().
I then tried to create the file manually:
folder = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); QString qdir = folder.path(); qdir.append(QString("/test.wav")); QFile file(qdir); if (file.exists()) file.remove(); qDebug() << "file exists: " << file.exists(); if (!file.exists()) { if (!folder.exists()) { folder.mkpath(folder.path()); } file.open(QIODevice::WriteOnly); file.write("Hello World"); file.close(); } if (file.exists()) { qDebug() << "file exists: " << file.exists(); file.open(QIODevice::ReadOnly); QByteArray data=file.readAll(); qWarning("file data=%s",data.constData()); file.close(); }
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) gives back:
/storage/0/emulated/Documentsfile.open(QIODevice::ReadOnly) gives back:
QIODevice::write (QFile, "/storage/emulated/0/Documents/transmission.wav"): device not openLooks like i am not able to create a file nor is the mediaRecorder. I also tried GenericDataLocation and other Locations.
I get D MediaRecorder: no match storage volume found! if i use HomeLocationInfos:
Htc one M8 (Andoird 6.0)
Permissions set: audio_record, read_external_storage, write_external_storage
target-SDK = API 26, Android 8.0
min-SDK = API 16, 4.1 -
Hi and welcome to devnet,
What version of Qt are you using ?
-
I have basically the exact same problem. Qt 5.9.4, Galaxy S7, Android 7.0. Can't make subfolders, or write files directly in the docs folder. Docs folder shows: "/storage/emulated/0/Documents". This folder does exist, though.
folder = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/subdir"; QDir dir(folder); if (!dir.exists()) { bool ok = dir.mkpath("."); qDebug() << "mkdir: " << ok; // <-- false }
-
What if you use
QStandardPaths::MusicLocation
? -
@SGaist said in Recording audio works on windows but not on android:
What if you use
QStandardPaths::MusicLocation
?Nope. Gives "/storage/emulated/0/Music". Anything under /storage/emulated/0 is write protected. However QStandardPaths::DataLocation --> "/data/user/0/<package name>/files" is writable, but I want it in the shared area. Obviously many other apps can write to that location, e.g. "Ol File Manager". Btw, I'm now on Qt 5.10.0.
-
Figured it out. Make pop up request for write permission if it doesn't have it in Qt 5.10:
# .pro file android { QT += androidextras }
#include <QtAndroidExtras/QtAndroid> ... QtAndroid::requestPermissionsSync( QStringList() << "android.permission.WRITE_EXTERNAL_STORAGE" );
-
This post is deleted!