Raspberry Pi 4 QMediaPlayer Audio Output via Analog Jack

Getting PyQt5 QMediaPlayer audio to play through the 3.5mm headphone jack on Raspberry Pi
Heads up! You've already completed this tutorial.

I'm using QMediaPlayer (PyQt5) to play video and audio files on the Raspberry Pi 4. This works perfectly when the sound goes through the HDMI port, but for my project the sound needs to come from the 3.5mm analog jack. Is this possible?

On the Raspberry Pi, PyQt5's QMediaPlayer uses GStreamer as its multimedia backend. GStreamer is a framework that handles decoding and playing audio and video. It's modular, meaning it relies on various plugins to support different audio systems.

By default, the Raspberry Pi OS installation may not include the GStreamer plugin for PulseAudio, which is the audio server that manages where sound gets routed — including to the 3.5mm analog output. Without this plugin, GStreamer falls back to sending audio over HDMI, since that path doesn't require PulseAudio.

The fix

Install the GStreamer PulseAudio plugin by running this command in your terminal:

bash
sudo apt install gstreamer1.0-pulseaudio

That's it. After installing this package, QMediaPlayer will be able to route audio through PulseAudio, which in turn sends it to whatever output device you've configured — including the analog jack.

You don't need to restart your Pi, but you will need to restart your Python application for the change to take effect.

Setting the audio output to the analog jack

Once the GStreamer PulseAudio plugin is installed, you also need to make sure your Raspberry Pi is configured to send audio to the analog output rather than HDMI. There are a couple of ways to do this.

Using raspi-config

Open a terminal and run:

bash
sudo raspi-config

Navigate to System OptionsAudio, and select the Headphones (analog) option. Then exit and confirm.

Using the desktop

If you're running the Raspberry Pi desktop environment, you can right-click the volume icon in the taskbar and select the analog output from there.

A minimal QMediaPlayer example

Here's a small working example that plays an audio file using QMediaPlayer in PyQt5. Once the GStreamer PulseAudio plugin is installed and your Pi is set to analog output, this will play through the 3.5mm jack. If you haven't already set up PyQt5 on your Raspberry Pi, see our guide on how to install PyQt5 on Raspberry Pi.

python
import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtWidgets import QApplication

app = QApplication(sys.argv)

player = QMediaPlayer()
player.setMedia(QMediaContent(QUrl.fromLocalFile("/path/to/your/audiofile.mp3")))
player.setVolume(80)
player.play()

sys.exit(app.exec_())

Replace "/path/to/your/audiofile.mp3" with the actual path to an audio file on your Pi.

If you want to build a more complete media player application, take a look at our Python multimedia player example for inspiration.

Summary

The root cause of this issue is a missing GStreamer plugin. PyQt5's QMediaPlayer on the Raspberry Pi relies on GStreamer for media playback, and without the PulseAudio plugin (gstreamer1.0-pulseaudio), audio can only be routed over HDMI. Installing the plugin gives GStreamer access to PulseAudio, which handles routing audio to the correct output — including the 3.5mm analog jack.

One install command and you're good to go:

bash
sudo apt install gstreamer1.0-pulseaudio

This saves you from needing hardware workarounds like HDMI audio extractors, and keeps everything neatly in software. Once you're up and running, you might want to explore packaging your PyQt5 application for distribution on Linux.

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

PyQt/PySide 1:1 Coaching with Martin Fitzpatrick

Save yourself time and frustration. Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.

Book Now 60 mins ($195)

Martin Fitzpatrick

Raspberry Pi 4 QMediaPlayer Audio Output via Analog Jack was written by Martin Fitzpatrick.

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.