4

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...

Nejat
  • 31,784
  • 12
  • 106
  • 138
Robert
  • 1,660
  • 22
  • 39

2 Answers2

6

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"));
Community
  • 1
  • 1
leetNightshade
  • 2,673
  • 2
  • 36
  • 47
3

You should play a file from disk; not a Qt resource. Since the resources are not supported yet. You can copy the file from resource to your hard drive on the fly and then play it :

QFile::copy(":/files/FarAway.mp3" , "/some/path/FarAway.mp3");
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • I see some said packaging those mp3 files into the bundle like this=> http://developer.nokia.com/community/discussion/showthread.php/212415-playing-mp3-using-QMediaPlayer-API/page2 Tried without success, what's the right way to do it? – Robert Jun 24 '14 at 05:46