Qt 6.5.3 on Android: No audio device detected
-
In my application I try to play some small WAV files. The code I wrote works on all operating systems except on Android. There I get the following error at the moment the audio file should be played:
W/TPanel : java.lang.NullPointerException: Attempt to invoke virtual method 'android.media.AudioDeviceInfo[] android.media.AudioManager.getDevices(int)' on a null object reference W/TPanel : at org.qtproject.qt.android.multimedia.QtAudioDeviceManager.getAudioDevices(QtAudioDeviceManager.java:181) W/TPanel : at org.qtproject.qt.android.multimedia.QtAudioDeviceManager.getAudioOutputDevices(QtAudioDeviceManager.java:74) W/TPanel : W/TPanel : No audio device detectedHere is the code snipped used to play the file:
#include <QtMultimediaWidgets/QVideoWidget> #include <QtMultimedia/QMediaPlayer> #include <QAudioOutput> #include <QMediaMetaData> #include <QMediaFormat> #include <QMediaDevices> /* ... */ void MainWindow::playSound(const string& file) { qtDebug() << "Playing file " << file; if (!mMediaPlayer) { mMediaPlayer = new QMediaPlayer(this); mAudioOutput = new QAudioOutput(this); mMediaPlayer->setAudioOutput(mAudioOutput); connect(mMediaPlayer, &QMediaPlayer::playingChanged, this, &MainWindow::onPlayingChanged); connect(mMediaPlayer, &QMediaPlayer::durationChanged, this, &MainWindow::onDurationChanged); connect(mMediaPlayer, &QMediaPlayer::metaDataChanged, this, &MainWindow::onMetaDataChanged); connect(mMediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &MainWindow::onMediaStatusChanged); connect(mMediaPlayer, &QMediaPlayer::errorOccurred, this, &MainWindow::onPlayerError); } mMediaPlayer->setSource(QUrl::fromLocalFile(QString::fromStdString(file))); if (!mMediaPlayer->isAvailable()) { qtDebug() << "WARNING: No audio modul found!"; return; } if (mMediaPlayer->isPlaying()) { mMediaPlayer->stop(); mMediaPlayer->setPosition(0); } mMediaPlayer->play(); }In header file the class is defined. Here a shortened excerpt:
#include <QMainWindow> #include <QMediaPlayer> class QAudioOutput; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(); ~MainWindow(); private slots: void onDurationChanged(qint64 duration); void onMetaDataChanged(); void onPlayingChanged(bool plying); void onPlayerError(QMediaPlayer::Error error, const QString &errorString); void onMediaStatusChanged(QMediaPlayer::MediaStatus status); private: void playSound(const std::string& file); void stopSound(); void muteSound(bool state); void setVolume(int volume); QMediaPlayer *mMediaPlayer{nullptr}; QAudioOutput *mAudioOutput{nullptr}; }I tried the player example and it worked. The code I wrote is based on the example. I checked the permissions and other settings and compared it with the example, but don't see any significant difference. Because the example works, the error must be in my code. But where?
A.T.