【问题标题】:Packaging mp3 files into Qt app将 mp3 文件打包到 Qt 应用程序中
【发布时间】:2014-08-14 11:04:56
【问题描述】:

由于我发现 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 创建器中执行和直接从应用程序执行有什么区别吗?比如调试模式之类的?

或者还有其他方法可以将 mp3 文件正确部署到包中吗?

【问题讨论】:

    标签: macos qt installation mp3 qmediaplayer


    【解决方案1】:

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

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

    您可以随意调用资源。

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

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

    【讨论】:

    • 谢谢@nicholas-smith。按照您的脚本,我将 PROJECT_ROOT/resources/sound 下的 mp3 文件复制到 Contents/MacOS/sound 中。
    【解决方案2】:

    这是我的最终解决方案。谢谢@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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2012-05-09
      • 2012-07-05
      • 1970-01-01
      • 2012-06-11
      相关资源
      最近更新 更多