7

How do I load a .mp3 file to use in a QMediaPlayer from a .qrc resource file?

This is what I have so far

QMediaPlayer *player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile(":/audio/theme.mp3"));
player->play();

resources.qrc:

<RCC>
    <qresource prefix="/audio">
        <file>theme.mp3</file>
    </qresource>
</RCC>

theme.mp3 is located in the project directory.

Oskar Persson
  • 6,605
  • 15
  • 63
  • 124
  • 2
    possible duplicate of [Play mp3 file in the resource with QMediaPlayer](http://stackoverflow.com/questions/24377720/play-mp3-file-in-the-resource-with-qmediaplayer) – leetNightshade Sep 11 '15 at 16:53

1 Answers1

17

Use m_player->setMedia(QUrl("qrc:/audio/theme.mp3"));

If you are using Qt Creator, you can copy this url to clipboard by pressing right button on the audio file in the side bar and choosing Copy url "...".

rsht
  • 1,522
  • 12
  • 27
  • I'm also having no sound (Qt 5.5) when I try to play from a resource. If I copy the file to a local file, then it works fine. – juzzlin Mar 16 '16 at 22:05
  • 4
    Great answer! thank you! And remember to remove `::fromLocalFile` when add `qrc:` to the path. – guan boshen Sep 26 '17 at 12:39