Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play mp3 file in the resource with QMediaPlayer

I try to play mp3 files declared in the resource, but it show:

Btn clicked
current media: "qrc://sound/sound/FarAway.mp3"
Error :  QMediaPlayer::FormatError
Media state :  QMediaPlayer::InvalidMedia

Here's how I set media:

player = new QMediaPlayer(this);
player->setMedia(QUrl(mediaFilePath)); 
qDebug() << "current media: " << player->currentMedia().canonicalUrl().toString();

connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), SLOT(handleStateChanged(QMediaPlayer::State)));
connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), SLOT(handleMediaStateChanged(QMediaPlayer::MediaStatus)));
connect(player, SIGNAL(error(QMediaPlayer::Error)), SLOT(handleError(QMediaPlayer::Error)));

According to this post it said QMediaPlayer need to be called play() after the callback of mediaStatusChanged(), which is what I've done exactly. So what's the problem???

P.S. I could play the mp3 file from the QMediaPlayer Example as local file.

UPDATE 1: I can play the mp3 file as local file...

like image 650
Robert Avatar asked Jan 10 '23 08:01

Robert


1 Answers

Your problem is now no longer a problem, you can play a Qt resource in the QMediaPlayer. Answer found here, and I'm confirming it here in case people are looking.

This code works for me when testing in my local project.

player->setMedia(QUrl("qrc:/audio/audio/Revival_Song01.mp3"));
like image 50
leetNightshade Avatar answered Jan 18 '23 22:01

leetNightshade