Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt 5.2.1 Android QMediaPlayer - no sound



  • I thought this is possible now with version 5.2.1 :

    QMediaPlayer *player = new QMediaPlayer();
    player->setMedia(QUrl("qrc:///sounds/sound1.wav"));
    player->setVolume(95);
    player->play();

    ([QTBUG-31422] The mediaplayer can now read files from the Qt Resource system is fixed in 5.2.1)

    but no sound on my nexus7 2013 (android 4.4) and samsung tab3 (android 4.2) devices,
    tried a .mp3 file, same result, no sound.
    This code works on Windows and Linux.

    Replacing qrc with assets didn't help either:
    player->setMedia(QUrl("assets:/sounds/sound1.wav"))

    I cheked the availability of the files in assets with the coding below:

    QFile inputFile;
    inputFile.setFileName("assets:/sounds/sound1.wav");
    if (not inputFile.open(QIODevice::ReadOnly)) {
    QMessageBox msgBox; msgBox.setText("wav file not found"); msgBox.exec();
    }

    So, how can i get sound to work ?



  • Making an alias to the sound resource file without the subdirectory (e.g "sound.wav") seems to work for me. Then replace the filename in setMedia with the alias:

    player->setMedia(QUrl(“qrc:///sound.wav”));

    Somehow having the resources in subdirectories, breaks the creation of the temporary file needed by QMediaPlayer in Android.



  • I made aliases for the sound resource files,
    and changed the setMedia as you wrote,
    but still no sound.



  • I've tested it successfully with mp3s on Android 4.4.2 and 4.1. On a 4.0.4 device it didn't work.
    If you only have .wav files you could try QSound::play"(:/sounds/sound1.wav”).

    Also check the volume levels of the device. setVolume doesn't seem to do anything on Android.



  • I've done al that, with no result.
    Added the code below, works fine under Windows, under Android
    it neglects this slot completely.

    @connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(statusChanged(QMediaPlayer::MediaStatus)));

    void playSound::statusChanged(QMediaPlayer::MediaStatus status)
    {
    qDebug() <<"In status changed "<<status;
    switch (status)
    {
    case QMediaPlayer::LoadedMedia:
    qDebug() <<"media loaded ";
    player->play();
    qDebug()<<"playerstatus "<<player->PlayingState;
    break;

     case QMediaPlayer::InvalidMedia:
          qDebug() <<"Invadid media ";
          break;
    
     default:
          break;
    

    }
    }
    @
    under Windows I get:
    In status changed QMediaPlayer::LoadedMedia
    media loaded
    playerstatus QMediaPlayer::PlayingState
    In status changed QMediaPlayer::BufferedMedia
    In status changed QMediaPlayer::EndOfMedia

    under Android : nothing



  • in my case I am using android api 10 and does not work, with api 17 if works great, I just said the debugger that there is nothing NULL:



  • I just found out that Qt Multimedia is not supported on Android (http://qt-project.org/wiki/Qt_for_Android_known_issues) :

    "The Qt Multimedia Widgets module is not supported on Android, which means video display is only available using the VideoOutput and Video QML items."

    So we have to wait for it to get fixed.



  • I'm not sure, QMediaplayer is included in QtMultimedia, and
    'Qt Multimedia Widgets provides additional multimedia-related widgets and controls. The classes expand the capabilities of the Qt Multimedia and Qt Widgets modules.'
    I think QtMultimediaWidgets is not necessary for playing wav/mp3 audio.
    I'm thinking about using JNI, see the link below.
    http://community.kde.org/Necessitas/JNI



  • The JNI audioplayer works flawlessly, I've got audio finally working!
    There are some drawbacks though,
    1)
    #include <qplatformnativeinterface.h> does not work, instead use
    #include <QtGui/5.2.1/QtGui/qpa/qplatformnativeinterface.h>
    2)
    it cannot read audiofiles from assets, so I copy them with QFile::copy
    from assets to a sdcard directory.
    3)
    several #ifdef Q_OS_ANDROID necessary in my code,
    because I use the QMediaPlayer for other platforms.

    I still don't understand why QMediaPlayer can't play my .wav files.



  • plis example janbur,



  • I followed the instructions on
    http://community.kde.org/Necessitas/JNI
    You can also download an example (the simple Audio player) from here.
    With the help of this example it's easy to see what you need to do
    to include the jni audioplayer in your own software



  • I tried the example, but i get following:

    @E/Qt ( 8841): ..\untitled7\androidmediaplayer.cpp:144 (jint JNI_OnLoad(JavaVM*, void*)): Can't find QSimpleAudioPlayer class
    W/dalvikvm( 8841): JNI_OnLoad returned bad version (-1) in /data/app-lib/org.qtproject.example.untitled7-1/libuntitled7.so 0x4260b580@

    Qt 5.2.1, Samsung Galaxy III, Android 4.3



  • Did you create the directory structure below, with the .jave file in it ?

    Create QSimpleAudioPlayer.java to your project_root/android/src/org/kde/necessitas/origo/

    I see that in my project it is without the android folder :
    project_root/src/org/kde/necessitas/origo/



  • I did it like in the example

    project_root/android/src/org/kde/necessitas/origo/

    it tried:
    project_root/src/org/kde/necessitas/origo/

    but the message is the same and the app crashes.



  • Hi,
    in may case it works properly with no errors my code:

    player->setMedia(QUrl("qrc:/new/prefix1/1.mp3")); // ok it works on windows //also works on android


Log in to reply