summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Hartmann <aha_1980@gmx.de>2018-03-24 13:21:18 +0100
committerAndré Hartmann <aha_1980@gmx.de>2018-04-25 08:10:13 +0000
commitfa8f2114bdb98160921711cd272ab1c9503ab9c3 (patch)
treecabc28a34c7a15fcfb4a0de50a2d06b680800f06
parent08dd127bd7c3900bdb9c73d646a12aae2383a9fb (diff)
downloadqtmultimedia-fa8f2114bdb98160921711cd272ab1c9503ab9c3.tar.gz
QSound: Allow to play files with qrc schema
It was possible to play resource files only by prefix ":/". Added support of qrc schema in filenames. Change-Id: I9e538422828ad2107ab5567d172dca8728cbc64d Reviewed-by: VaL Doroshchuk <valentyn.doroshchuk@qt.io>
-rw-r--r--src/multimedia/audio/qsound.cpp11
-rw-r--r--tests/auto/integration/qsound/qsound.pro3
-rw-r--r--tests/auto/integration/qsound/resources.qrc5
-rw-r--r--tests/auto/integration/qsound/tst_qsound.cpp22
4 files changed, 40 insertions, 1 deletions
diff --git a/src/multimedia/audio/qsound.cpp b/src/multimedia/audio/qsound.cpp
index b6cf49671..7ad30608d 100644
--- a/src/multimedia/audio/qsound.cpp
+++ b/src/multimedia/audio/qsound.cpp
@@ -60,6 +60,9 @@
\snippet multimedia-snippets/qsound.cpp 1
+ In both cases, the file may either be a local file or in a
+ \l{The Qt Resource System}{resource}.
+
Once created a QSound object can be queried for its fileName() and
total number of loops() (i.e. the number of times the sound will
play). The number of repetitions can be altered using the
@@ -88,6 +91,8 @@
/*!
Plays the sound stored in the file specified by the given \a filename.
+ The file can either be a local file or in a \l{The Qt Resource System}{resource}.
+
\sa stop(), loopsRemaining(), isFinished()
*/
void QSound::play(const QString& filename)
@@ -104,13 +109,17 @@ void QSound::play(const QString& filename)
Constructs a QSound object from the file specified by the given \a
filename and with the given \a parent.
+ The file can either be a local file or in a \l{The Qt Resource System}{resource}.
+
\sa play()
*/
QSound::QSound(const QString& filename, QObject* parent)
: QObject(parent)
{
m_soundEffect = new QSoundEffect(this);
- m_soundEffect->setSource(QUrl::fromLocalFile(filename));
+ const bool isQrc = filename.startsWith(QLatin1String("qrc:"), Qt::CaseInsensitive);
+ const QUrl url = isQrc ? QUrl(filename) : QUrl::fromLocalFile(filename);
+ m_soundEffect->setSource(url);
}
/*!
diff --git a/tests/auto/integration/qsound/qsound.pro b/tests/auto/integration/qsound/qsound.pro
index aa8e5e409..1b552c60e 100644
--- a/tests/auto/integration/qsound/qsound.pro
+++ b/tests/auto/integration/qsound/qsound.pro
@@ -8,3 +8,6 @@ CONFIG += testcase
SOURCES += tst_qsound.cpp
TESTDATA += test.wav
+
+RESOURCES += \
+ resources.qrc
diff --git a/tests/auto/integration/qsound/resources.qrc b/tests/auto/integration/qsound/resources.qrc
new file mode 100644
index 000000000..b54c65040
--- /dev/null
+++ b/tests/auto/integration/qsound/resources.qrc
@@ -0,0 +1,5 @@
+<RCC>
+ <qresource prefix="/">
+ <file>test.wav</file>
+ </qresource>
+</RCC>
diff --git a/tests/auto/integration/qsound/tst_qsound.cpp b/tests/auto/integration/qsound/tst_qsound.cpp
index 9fcf79184..dbf75f2e3 100644
--- a/tests/auto/integration/qsound/tst_qsound.cpp
+++ b/tests/auto/integration/qsound/tst_qsound.cpp
@@ -46,6 +46,9 @@ private slots:
void testPlay();
void testStop();
+ void testPlayResource_data();
+ void testPlayResource();
+
void testStaticPlay();
private:
@@ -110,6 +113,25 @@ void tst_QSound::testStop()
QTRY_VERIFY(sound->isFinished());
}
+void tst_QSound::testPlayResource_data()
+{
+ QTest::addColumn<QString>("filePath");
+
+ QTest::newRow("prefix :/") << ":/test.wav";
+ QTest::newRow("prefix qrc:") << "qrc:test.wav";
+ QTest::newRow("prefix qrc:///") << "qrc:///test.wav";
+}
+
+void tst_QSound::testPlayResource()
+{
+ QFETCH(QString, filePath);
+
+ QSound snd(filePath);
+ snd.play();
+ QVERIFY(!snd.isFinished());
+ QTRY_VERIFY(snd.isFinished());
+}
+
void tst_QSound::testStaticPlay()
{
// Check that you hear sound with static play also.