I am trying to play a sound from my application, which is written in C++ Qt5. Followed an example how to play audio on Torizon OS using ALSA. Looks like ALSA is loaded correctly inside the container, since I can play the sound using aplay command from within the container. However, no sound is played from the app. I also see alsa plugin in the correct directory within the container (/usr/lib/arm-linux-gnueabihf/qt5/plugins/audio/).
I am also seeing the errors about pulseaudio in my log, like this:
“PulseAudioService: Connection failure: Connection refused”
I set environmental variables in Dockerfile to use ALSA instead of pulseaudio, but Qt might still be defaulting to pulseaudio.
I would like to get some help on how to setup Qt application to play sound using ALSA please.
Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!
Did you like my answer?
Hello,
It sounds like you’re facing a common issue where your Qt5 application is defaulting to PulseAudio instead of ALSA on Torizon OS. Let’s go through some steps to help you configure your application to use ALSA for audio playback:
Environment Variables: Double-check that the environment variables in your Dockerfile are set to prioritize ALSA. You can set the QT_AUDIO_BACKEND environment variable to alsa to explicitly instruct Qt to use ALSA.
Qt Configuration: Ensure your Qt application is configured to use ALSA. This might involve checking the Qt multimedia backend settings. You can inspect the qt.conf file or set the appropriate environment variables at runtime.
ALSA Configuration: Verify that ALSA is correctly configured within your container. Use tools like alsamixer to check and adjust audio settings. Ensure the correct sound card is selected and that the audio output is not muted.
Access to Sound Devices: Make sure your container has access to the necessary sound devices. This typically involves adding /dev/snd to the list of devices your container can access. You can specify this in your Docker run command or Dockerfile.
PulseAudio Errors: The “Connection refused” error suggests PulseAudio might still be running or being called by default. Ensure PulseAudio is not installed or running in your container if you intend to use ALSA exclusively.
Testing: Use aplay within the container to test audio playback independently of your application. This can help confirm that ALSA is functioning correctly.
By following these steps, you should be able to configure your Qt application to use ALSA for audio playback. If issues persist, check the logs for any additional error messages that might provide further insight.
I hope this helps! If you have any more questions, feel free to ask.
Best regards,
Toradex AI Assistant
Please, also check if these links can help you solve your problem:
Toradex AI hints are good and I also suggest looking at the community thread recommended there. Can you have a look and see if this is helpful to you? Keep in mind that the thread is a bit old and support for audio in Qt might have changed. Despite that note, the discussion still gives insightful tweaks and tests.
Hi @danilo.tx, thank you for your response. I followed the steps provided by Toradex AI and I no longer see any errors about Pulse Audio, and I verified that it’s not installed or running in my container. I can use aplay within the container independently from my application and it plays the sound. However, inside the application it still doesn’t. I tried using QSoundEffect class to play the sound in my application, and I tried following the recommended community thread and play the sound using QAudioOutput class. With the QSoundEffect the sound just doesn’t play, no errors. With QAudioOutput I get an error “QAudioOutput: snd_pcm_hw_params_set_buffer_time_near: err = -22”
Hi @ycherapukha , thanks for sharing your progress.
Having aplay being able to play sounds from within your container is good: means that hardware, OS and container are correctly configured.
Now, since you are having issues specifically with Qt, we recommend you to refer to Qt’s documentation and forum, since they will have lots more information and tips for you specially for your Qt use case.