1

I'm using Qt Creator to design a game, trying to add a simple audio player to play whenever the function is called. The game will be used on another computer but the entire Snake directory will be moved so I'm wondering the best way to code this so that it can be played anywhere with the audio.

The mp3 is located in the sub directory "scripts/byte.mp3" but I'm wondering if I'm misusing the URL pathing. I've tried "./scripts/byte.mp3" as well as "//scripts/byte.mp3" but it just can't seem to find the file unless I do the entire path. Wondering what is the right way to code it to find the file.

void GLWidget::playAudio()
{
    audioPlayer->setMedia(QUrl::fromLocalFile("C:/Users/Jim/Documents/Snake/sounds/byte.mp3"));
    audioPlayer->setVolume(50);
    audioPlayer->play();
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Maybe you can try using something like [QCoreApplication::applicationDirPath](http://doc.qt.io/qt-4.8/qcoreapplication.html#applicationDirPath). – wendelbsilva Nov 20 '15 at 20:21
  • Try `"scripts/byte.mp3"`. Use this API to see what the working directory is https://msdn.microsoft.com/en-us/library/windows/desktop/aa364934%28v=vs.85%29.aspx – brettwhiteman Nov 20 '15 at 20:21
  • This may help http://stackoverflow.com/questions/4807629/how-do-i-find-the-current-directory – brettwhiteman Nov 20 '15 at 20:22
  • If you do a relative path. Then you need to know what directory your application thinks it is currently running it. This is not the same as where the executable lives. Try [`getcwd()`](http://linux.die.net/man/3/getcwd) to see which directory your executable thinks it is running in. – Martin York Nov 20 '15 at 20:24
  • Tried all these, nothing seemed to work. I think it's cause once it's built and I run it it's referring to the built directory and the file isn't contained there. Only the cpp and hpp files. – Psychosupreme Nov 20 '15 at 20:29
  • I'll attempt using a resource file. See if that fixes things. – Psychosupreme Nov 20 '15 at 20:33

0 Answers0