Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Recording audio works on windows but not on android
Forum Update on Monday, May 27th 2025

Recording audio works on windows but not on android

Scheduled Pinned Locked Moved Unsolved Game Development
7 Posts 3 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Desard
    wrote on last edited by
    #1

    Hey guys, i want to record and save audio in a file. Here is the code is use:
    audioRecorder.h

    private: 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/Documents

    file.open(QIODevice::ReadOnly) gives back:
    QIODevice::write (QFile, "/storage/emulated/0/Documents/transmission.wav"): device not open

    Looks 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 HomeLocation

    Infos:
    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      What version of Qt are you using ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • operamintO Offline
        operamintO Offline
        operamint
        wrote on last edited by
        #3

        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
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What if you use QStandardPaths::MusicLocation ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          operamintO 1 Reply Last reply
          0
          • SGaistS SGaist

            What if you use QStandardPaths::MusicLocation ?

            operamintO Offline
            operamintO Offline
            operamint
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • operamintO Offline
              operamintO Offline
              operamint
              wrote on last edited by
              #6

              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" );
              
              1 Reply Last reply
              1
              • SGaistS SGaist

                Hi and welcome to devnet,

                What version of Qt are you using ?

                D Offline
                D Offline
                Desard
                wrote on last edited by Desard
                #7
                This post is deleted!
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved