0

I am using Qt Quick Application project in QT 5.2.1 in Linux Mint 13 and I want use relative path to my audio files, which are located in sounds folder of my project folder. Here is my

main.qml

import QtQuick 2.2
import QtQuick.Window 2.1
import QtMultimedia 5.0

Window {
    visible: true
    width: 360
    height: 360
    Audio{
        id: sound
        source: "sounds/1.mp3"  //doesn't work
        source: "file:../sounds/1.mp3" //also doesn't work
        source: "file:/home/vlado/Qt projects/test521/sounds/1.mp3" //this works
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            sound.play()
        }
    }

    Text {
        text: qsTr("Play Sound")
        anchors.centerIn: parent
    }
}

Doesn't work means, that there is following error:

GStreamer; Unable to play - "" Error: "No URI set"

As you can see, everything is OK, if I use absolute path, but I would like to deploy this application and installation folder then will be different. That's why I want to use relative path. Here is my

test521.pro

TEMPLATE = app

QT += qml quick

SOURCES += main.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

Here is

my main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));

    return app.exec();
}

How to set relative path instead of absolute?

Edit: Solution for QML: how to specify image file path relative to application folder doesn't work for me.

Community
  • 1
  • 1
Vladimír
  • 701
  • 1
  • 7
  • 24
  • See: [QML: how to specify image file path relative to application folder](http://stackoverflow.com/questions/27549708/qml-how-to-specify-image-file-path-relative-to-application-folder). – agold Oct 10 '15 at 17:05
  • @agold but in accepted solution line engine.rootContext()->setContextProperty("applicationDirPath", QGuiApplication::applicationDirPath()); gives me an error: invalid use of incomplete type 'struct QQmlContext' – Vladimír Oct 10 '15 at 17:26
  • You should add `#include `, see [Error: forward declaration of 'class QQmlContext'](https://forum.qt.io/topic/30025/solved-error-forward-declaration-of-class-qqmlcontext/2). – agold Oct 10 '15 at 17:34
  • @agold QQmlContext error solved. Thanks! But now with line source: applicationDirPath + "/../resources/sounds/1.mp3" I still have the error: GStreamer; Unable to play - "" Error: "No URI set" The same error is, if I change this line to source: applicationDirPath + "file:../sounds/1.mp3" – Vladimír Oct 10 '15 at 17:57
  • Put the path after "`file:`", thus: `source: "file: "+applicationDirPath+"/../sounds/1.mp3"` – agold Oct 10 '15 at 18:02
  • No. It doesn't work. It gives me an error: GStreamer; Unable to pause - "file: /home/vlado/Qt projects/build-test521-Desktop_Qt_5_2_1_GCC_32bit-Debug/../sounds/1.mp3" Error: "Could not open resource for reading." I updated line to source: "file:"+applicationDirPath+"/sounds/1.mp3" and also I have error: GStreamer; Unable to pause - "file:///home/vlado/Qt projects/build-test521-Desktop_Qt_5_2_1_GCC_32bit-Debug/sounds/1.mp3" Error: "Resource not found." I think it is because qt looks for 1.mp3 in debug folder and there is no such file. – Vladimír Oct 10 '15 at 18:13
  • As a side note you can use your sounds, images etc as resources just like the qml files. Then just use the qrc path to access your files. – juzzlin Oct 10 '15 at 18:36
  • @Vladimir: [here](http://stackoverflow.com/questions/26305646/video-wont-play) they comment about a [bug which is fixed in Qt 5.5](https://bugreports.qt.io/browse/QTBUG-33400). – agold Oct 10 '15 at 19:21
  • @juzzlin it also doesn't work. If I use source:"qrc:/1.mp3" and 1.mp3 is added in Resources I get Error: "No valid frames found before end of stream" – Vladimír Oct 11 '15 at 22:23
  • @agold with Qt 5.5 it's worse then with 5.2.1, because when I am starting to run project I get defaultServiceProvider::requestService(): no service found for - "org.qt-project.qt.mediaplayer" – Vladimír Oct 11 '15 at 22:26
  • Be sure you have the QtMediaplayer compiled, see: http://stackoverflow.com/questions/22966150/qtquick-cannot-playback-any-video-on-embedded-linux – agold Oct 12 '15 at 08:46
  • @agold I did not understand how to compile QtMultimediaplayer. Can you advise to me? – Vladimír Oct 18 '15 at 23:57

0 Answers0