将mp3文件打包到Qt应用程序中

时间:2014-06-24 08:57:09

标签: macos qt install mp3 qmediaplayer

由于我发现QMediaPlayer无法从资源中检索到mp3文件,因此我想出来将它们用作本地文件。我使用this cmd进行了一些修改,将文件复制到安装目录中:

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

所以这些文件存在于/HomeControl.app/Contents/MacOS/sound中。从Qt Creator开始,应用程序可以完美地播放mp3,但如果从构建目录执行,它根本不会播放mp3!真的不知道为什么。

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"

在Qt创作者和执行中执行是否有任何区别?直接来自App?像调试模式还是什么?

还是有其他方法可以正确地将mp3文件部署到捆绑包中吗?

2 个答案:

答案 0 :(得分:2)

这就是我为OS X捆绑包构建资源的方法:

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

您可以随意调用资源。

将它们复制到Contents/MacOS/dirInTheProjectDirectory。然后,您可以使用QDir(QCoreApplication::applicationDirPath()+"/dirInTheProjectDirectory/");

访问它们

可能的问题是,当您在代码中访问它们时,您指向构建目录中的路径,而不是指向捆绑包中的路径。

答案 1 :(得分:0)

这是我的最终解决方案。谢谢@ nicholas-smith。

在.pro文件中:

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

然后使用以下方法访问这些文件:

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

从日志显示:

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"