Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dambientsound.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-3.0-only
6#include <QAudioFormat>
7#include <qdir.h>
8#include <QQmlContext>
9#include <QQmlFile>
10
12
13/*!
14 \qmltype AmbientSound
15 \inqmlmodule QtQuick3D.SpatialAudio
16 \ingroup quick3d_spatialaudio
17 \ingroup multimedia_audio_qml
18
19 \brief A stereo overlay sound.
20
21 A AmbientSound represents a position and orientation independent sound.
22 It's commonly used for background sounds (e.g. music) that is supposed to be independent
23 of the listeners position and orientation.
24 */
25
26QQuick3DAmbientSound::QQuick3DAmbientSound()
27{
28 m_sound = new QAmbientSound(QQuick3DAudioEngine::getEngine());
29
30 connect(m_sound, &QAmbientSound::sourceChanged, this, &QQuick3DAmbientSound::sourceChanged);
31 connect(m_sound, &QAmbientSound::volumeChanged, this, &QQuick3DAmbientSound::volumeChanged);
32 connect(m_sound, &QAmbientSound::loopsChanged, this, &QQuick3DAmbientSound::loopsChanged);
33 connect(m_sound, &QAmbientSound::autoPlayChanged, this, &QQuick3DAmbientSound::autoPlayChanged);
34}
35
37{
38 delete m_sound;
39}
40
41/*!
42 \qmlproperty url AmbientSound::source
43
44 The source file for the sound to be played.
45 */
47{
48 return m_sound->source();
49}
50
52{
53 const QQmlContext *context = qmlContext(this);
54 QUrl url;
55 if (context) {
56 url = context->resolvedUrl(source);
57 } else {
58 url = QUrl::fromLocalFile(QDir::currentPath() + u"/");
59 url = url.resolved(source);
60 }
61 m_sound->setSource(url);
62}
63
64/*!
65 \qmlproperty real AmbientSound::volume
66
67 Defines an overall volume for this sound source.
68 */
70{
71 m_sound->setVolume(volume);
72}
73
75{
76 return m_sound->volume();
77}
78
79/*!
80 \qmlproperty int AmbientSound::loops
81
82 Determines how often the sound is played before the player stops.
83 Set to QAmbienSound::Infinite to loop the current sound forever.
84
85 The default value is \c 1.
86 */
87int QQuick3DAmbientSound::loops() const
88{
89 return m_sound->loops();
90}
91
93{
94 m_sound->setLoops(loops);
95}
96
97/*!
98 \qmlproperty bool AmbientSound::autoPlay
99
100 Determines whether the sound should automatically start playing when a source
101 gets specified.
102
103 The default value is \c true.
104 */
106{
107 return m_sound->autoPlay();
108}
109
111{
112 m_sound->setAutoPlay(autoPlay);
113}
114
115/*!
116 \qmlmethod AmbientSound::play()
117
118 Starts playing back the sound. Does nothing if the sound is already playing.
119 */
121{
122 m_sound->play();
123}
124
125/*!
126 \qmlmethod AmbientSound::pause()
127
128 Pauses sound playback at the current position. Calling play() will continue playback.
129 */
131{
132 m_sound->pause();
133}
134
135/*!
136 \qmlmethod AmbientSound::stop()
137
138 Stops sound playback and resets the current position and loop count to 0. Calling play() will
139 begin playback at the beginning of the sound file.
140 */
142{
143 m_sound->stop();
144}
145
146QT_END_NAMESPACE
void setAutoPlay(bool autoPlay)
void pause()
\qmlmethod AmbientSound::pause()
void stop()
\qmlmethod AmbientSound::stop()
bool autoPlay() const
\qmlproperty bool AmbientSound::autoPlay
QUrl source() const
\qmlproperty url AmbientSound::source
void setVolume(float volume)
\qmlproperty real AmbientSound::volume