2

Since I found mp3 files can't be retrieved by QMediaPlayer from resource, I figure it out to use them as local file. I use this cmd with a little modification to copy files into installation directory:

copydata.commands = $(COPY_DIR) $$PWD/resources/sound $$OUT_PWD/HomeControl.app/Contents/MacOS
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata

So those files existed in the /HomeControl.app/Contents/MacOS/sound. From Qt Creator the app plays the mp3 perfectly, but if executed from the build directory it doesn't play the mp3 at all! Really don't know why.

arm away btn clicked
current media:  "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Arm_Away.mp3"
disarm stay btn clicked
current media:  "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Disarmed.mp3"

Is there any difference from executing in the Qt creator & directly from the App? Like debug mode or what?

Or is there other ways to correctly deploy mp3 files into the bundle?

Robert
  • 1,660
  • 22
  • 39

2 Answers2

2

This is how I build in resources for OS X bundles:

mac {
    Resources.files = dirInTheProjectDirectory
    Resources.path = Contents/MacOS
    QMAKE_BUNDLE_DATA += Resources
}

You can call Resources whatever you fancy.

That copies them to Contents/MacOS/dirInTheProjectDirectory. You can then access them with QDir(QCoreApplication::applicationDirPath()+"/dirInTheProjectDirectory/");

The likely problem is when you're accessing them in your code you're pointing to the path in the build directory, and not the path that's within your bundle.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55
  • Thanks @nicholas-smith. Follow your script I copy the mp3 files under PROJECT_ROOT/resources/sound into Contents/MacOS/sound. – Robert Jun 24 '14 at 13:49
0

Here's my final solution. Thank you @nicholas-smith.

In the .pro file:

# deploy with mp3 folder
mac {
    Resources.files = ./resources/sound
    Resources.path = Contents/MacOS
    QMAKE_BUNDLE_DATA += Resources
}

Then accessing those file with:

// pass the path of mp3 to the QMediaPlayer
playAudio(QCoreApplication::applicationDirPath()
    + "/sound/System_Disarmed.mp3");

From log it shows:

arm away btn clicked
current media:  "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Arm_Away.mp3"
Robert
  • 1,660
  • 22
  • 39