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
3
5
6#include <QtQuick3DSpatialAudio/private/qquick3daudioengine_p.h>
7#include <QtSpatialAudio/qambientsound.h>
8
9#include <QtCore/qdir.h>
10#include <QtQml/qqmlcontext.h>
11#include <QtQml/qqmlfile.h>
12
14
15/*!
16 \qmltype AmbientSound
17 \inqmlmodule QtQuick3D.SpatialAudio
18 \ingroup quick3d_spatialaudio
19 \ingroup multimedia_audio_qml
20
21 \brief A stereo overlay sound.
22
23 A AmbientSound represents a position and orientation independent sound.
24 It's commonly used for background sounds (e.g. music) that is supposed to be independent
25 of the listeners position and orientation.
26 */
27
28QQuick3DAmbientSound::QQuick3DAmbientSound()
29{
30 m_sound = new QAmbientSound(QQuick3DAudioEngine::getEngine());
31
32 connect(m_sound, &QAmbientSound::sourceChanged, this, &QQuick3DAmbientSound::sourceChanged);
33 connect(m_sound, &QAmbientSound::volumeChanged, this, &QQuick3DAmbientSound::volumeChanged);
34 connect(m_sound, &QAmbientSound::loopsChanged, this, &QQuick3DAmbientSound::loopsChanged);
35 connect(m_sound, &QAmbientSound::autoPlayChanged, this, &QQuick3DAmbientSound::autoPlayChanged);
36}
37
39{
40 delete m_sound;
41}
42
43/*!
44 \qmlproperty url AmbientSound::source
45
46 The source file for the sound to be played.
47 */
49{
50 return m_sound->source();
51}
52
54{
55 const QQmlContext *context = qmlContext(this);
56 QUrl url;
57 if (context) {
58 url = context->resolvedUrl(source);
59 } else {
60 url = QUrl::fromLocalFile(QDir::currentPath() + u"/");
61 url = url.resolved(source);
62 }
63 m_sound->setSource(url);
64}
65
66/*!
67 \qmlproperty real AmbientSound::volume
68
69 Defines an overall volume for this sound source.
70 */
72{
73 m_sound->setVolume(volume);
74}
75
77{
78 return m_sound->volume();
79}
80
81/*!
82 \qmlproperty int AmbientSound::loops
83
84 Determines how often the sound is played before the player stops.
85 Set to QAmbienSound::Infinite to loop the current sound forever.
86
87 The default value is \c 1.
88 */
89int QQuick3DAmbientSound::loops() const
90{
91 return m_sound->loops();
92}
93
95{
96 m_sound->setLoops(loops);
97}
98
99/*!
100 \qmlproperty bool AmbientSound::autoPlay
101
102 Determines whether the sound should automatically start playing when a source
103 gets specified.
104
105 The default value is \c true.
106 */
108{
109 return m_sound->autoPlay();
110}
111
113{
114 m_sound->setAutoPlay(autoPlay);
115}
116
117/*!
118 \qmlmethod void AmbientSound::play()
119
120 Starts playing back the sound. Does nothing if the sound is already playing.
121 */
123{
124 m_sound->play();
125}
126
127/*!
128 \qmlmethod void AmbientSound::pause()
129
130 Pauses sound playback at the current position. Calling play() will continue playback.
131 */
133{
134 m_sound->pause();
135}
136
137/*!
138 \qmlmethod void AmbientSound::stop()
139
140 Stops sound playback and resets the current position and loop count to 0. Calling play() will
141 begin playback at the beginning of the sound file.
142 */
144{
145 m_sound->stop();
146}
147
148QT_END_NAMESPACE
void setAutoPlay(bool autoPlay)
void pause()
\qmlmethod void AmbientSound::pause()
void stop()
\qmlmethod void AmbientSound::stop()
bool autoPlay() const
\qmlproperty bool AmbientSound::autoPlay
QUrl source() const
\qmlproperty url AmbientSound::source
void setVolume(float volume)
\qmlproperty real AmbientSound::volume
Combined button and popup list for selecting options.