Playing sound with Qt in Python: Error decoding source

2022-07-11

The correct way for playing sound with PySide6 and fixing the "error decoding source".

 

Qt is a widely adopted GUI framework, but its documentation is quite awful, especially when working in Python. And it's likely that you get the error

QSoundEffect(qaudio): Error decoding source /path/to/the/file
text

when trying to play some sound effects with Qt. This error message is quite misleading. Apart from the obvious circumstance that the audio file is broken, there might be some other causes.

Cause 1: You are trying to play an MP3 file #

MP3 files are not supported. You can convert the MP3 file to a WAV file:

ffmpeg -i ding.mp3 ding.wav
sh

Cause 2: You are passing the path, not URL #

QSoundEffect.setSource is expecting an URL, not a Path or str. So any one of these works:

self.soundEffect.setSource(
    QtCore.QUrl.fromLocalFile(Path(__file__).parent / "ding.wav")
)
self.soundEffect.setSource(QtCore.QUrl.fromLocalFile("./ding.wav"))
self.soundEffect.setSource("file:./ding.wav")
python

But none of these works:

self.soundEffect.setSource("ding.wav")
self.soundEffect.setSource(str(Path(__file__).parent / "ding.wav"))
python

Appendix: Full code #

import sys
from pathlib import Path

from PySide6 import QtCore, QtMultimedia, QtWidgets


class MyWidget(QtWidgets.QWidget):
    def __init__(self, parent=None) -> None:
        super().__init__(parent)

        mainLayout = QtWidgets.QVBoxLayout(self)
        self.soundButton = QtWidgets.QPushButton(text="Play")
        mainLayout.addWidget(self.soundButton)

        self.soundEffect = QtMultimedia.QSoundEffect()
        self.soundEffect.setSource("file:./ding.wav")
        self.soundEffect.setLoopCount(3)

        self.soundButton.clicked.connect(self.soundEffect.play)


if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    widget = MyWidget()
    widget.resize(200, 100)
    widget.show()
    sys.exit(app.exec())
python
safosaffers
safosaffers
2025-03-23

Thanks for the instructions, @AllanChain. At least, the sound (with a start delay of 5 seconds) is playing, but I am receiving errors:

qt.multimedia.ffmpeg: Using Qt multimedia with FFmpeg version 7.1 LGPL version 2.1 or later qt.multimedia.ffmpeg: Available HW decoding frameworks: qt.multimedia.ffmpeg: d3d11va qt.multimedia.ffmpeg: dxva2 qt.multimedia.ffmpeg: d3d12va qt.multimedia.ffmpeg: Available HW encoding frameworks: qt.multimedia.ffmpeg: d3d11va qt.multimedia.ffmpeg: dxva2 qt.multimedia.ffmpeg: d3d12va [SWR @ 000001DF2CCE5500] Output channel layout "" is invalid or unsupported. [SWR @ 000001DF2CCE5500] Context has not been initialized

For your code in the "Appendix: Complete code", I also receive this. The MP3 sound is received as:

ffmpeg -i "ding.mp3" "ding.wav"

My ffmpeg version:

ffmpeg version n7.1-214-g71889a8437-20250228

. How can I fix or suppress these issues?

AllanChain
AllanChain
2025-03-24

How can I fix or suppress these issues?

Sorry, I'm not a Qt expert and I don't know how to solve this. In my experience, having some lines of warning printed is normal and is not necessarily related to your delay. You can try a shorter sound and smaller sound file and see if the issue persists.

safosaffers
safosaffers
2025-03-24

You can try a shorter sound and smaller sound file and see if the issue persists.

Thanks for the quick reply, @AllanChain. Unfortunately my audio is 500 kb (less than a second). I already found out that the problem does not occur if you use PyQt5 (it occurs in PySide6 and PyQt6, the latest stable versions at the moment). If possible, please tell me the version of PySide6 that you used for the example.

AllanChain
AllanChain
2025-03-24

I tried with pyside6==6.8.2.1 on my Linux machine and there is no delay.

Here is the ding.wav file (GitHub does not allow .wav files, so I gzipped it): ding.wav.gz

And there is no warning in the console:

qt.multimedia.ffmpeg: Using Qt multimedia with FFmpeg version 7.1 LGPL version 2.1 or later
qt.multimedia.ffmpeg: Available HW decoding frameworks:
qt.multimedia.ffmpeg:      vaapi
qt.multimedia.ffmpeg: Available HW encoding frameworks:
qt.multimedia.ffmpeg:      vaapi
text
safosaffers
safosaffers
2025-03-24

@AllanChain, I tested it on Linux (Ubuntu 24 LTS), and the error did not occur. Previously, the error was happening when running on Windows 11. Since I used a virtual environment via venv on both operating systems, the issue might be related to cross-platform differences in QtMultimedia or OS-specific behavior. Thanks for your support!😌

Leave your comments and reactions on GitHub