How to play sounds using QMediaPlayer
-
I added this:
connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, [this] (QMediaPlayer::MediaStatus status) -> void { qDebug() << "QMediaPlayer::mediaStatusChanged" << status; });
But I see this message only:
widget.cpp:13: warning: Lambda capture 'this' is not used (fix available)
-
My example from the topic works with Qt5 but doesn't work with Qt6.
-
From Changes to Qt Multimedia in Qt 6:
Audio inputs and output QMediaPlayer and QMediaCaptureSession (and the corresponding QML types MediaPlayer and CaptureSession) are not connected to any audio devices by default. Explicitly connect them to a QAudioInput/AudioInput or QAudioOutput/AudioOutput to capture or play back audio.
You're not doing that in your code and I forgot to mention that I did so in my test; something like:
mediaPlayer.setAudioOutput(new QAudioOutput);
If this still does not fix it, try an absolute path just to verify it's a file access issue or not.
-
@Abderrahmene_Rayene said in How to play sounds using QMediaPlayer:
new QAudioOutput
Yes, it helped! Thank you very much! Interesting that this didn't work with
qrc://
and works withqrc:/
#include "widget.h" #include <QtCore/QUrl> #include <QtWidgets/QPushButton> #include <QtMultimedia/QAudioOutput> Widget::Widget(QWidget *parent) : QWidget(parent) { mediaPlayer.setSource(QUrl("qrc:/assets/audio/music.wav")); mediaPlayer.setAudioOutput(new QAudioOutput); QPushButton *playButton = new QPushButton("Play", this); connect(playButton, &QPushButton::pressed, this, &Widget::play); connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, [this] (QMediaPlayer::MediaStatus status) -> void { qDebug() << "QMediaPlayer::mediaStatusChanged" << status; }); } Widget::~Widget() { } void Widget::play() { mediaPlayer.play(); qDebug() << "play"; }
-
8 8Observer8 has marked this topic as solved on
-
@8Observer8 said in How to play sounds using QMediaPlayer:
connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, [this] (QMediaPlayer::MediaStatus status) -> void { qDebug() << "QMediaPlayer::mediaStatusChanged" << status; });
Does the following work?
connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, [=](QMediaPlayer::MediaStatus status) -> void{ qDebug() << "QMediaPlayer::mediaStatusChanged" << status;} );
-
8 8Observer8 referenced this topic on
-
Hello guys again. I want to rewrite the example above to PySide6 and PyQt6:
from PySide6.QtCore import QUrl from PySide6.QtMultimedia import QAudioOutput, QMediaPlayer from PySide6.QtWidgets import QPushButton, QWidget class Widget(QWidget): def __init__(self): super().__init__() self.mediaPlayer = QMediaPlayer() self.mediaPlayer.setSource(QUrl("assets/audio/music.wav")) self.mediaPlayer.setAudioOutput(QAudioOutput()) self.mediaPlayer.mediaStatusChanged.connect(lambda status : print(status)) playButton = QPushButton("Play", self) playButton.clicked.connect(self.play) def play(self): self.mediaPlayer.play() print("play")
import sys from PySide6.QtCore import Qt from PySide6.QtWidgets import QApplication from widget import Widget if __name__ == "__main__": app = QApplication(sys.argv) w = Widget() w.show() sys.exit(app.exec())
But I have these errors:
MediaStatus.InvalidMedia
MediaStatus.LoadingMedia
play
MediaStatus.InvalidMediaThe path to the audio file is correct:
-
I tried to use
fromLocalFile
but I have the same result:self.mediaPlayer.setSource(QUrl.fromLocalFile("assets/audio/music.wav"))
-
QUrl("assets/audio/music.wav")
Test with an absolute path rather than a relative one. Not sure what the current directory is when running Python script, so check this is not the issue?
-
But if I write the wrong path, I get a different error message:
self.mediaPlayer.setSource(QUrl("wrong_path/audio/music.wav"))
handleSourceError: 0x80070003 MediaStatus.InvalidMedia MediaStatus.LoadingMedia play handleSourceError: 0x80070003 MediaStatus.InvalidMedia
-
I solved the problem! I found the solution here: https://zhuanlan.zhihu.com/p/521028351
self.audioOutput = QAudioOutput() self.mediaPlayer.setAudioOutput(self.audioOutput)