Qt 4.8
qsound.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsound.h"
43 
44 #ifndef QT_NO_SOUND
45 
46 #include "qlist.h"
47 #include <private/qobject_p.h>
48 #include "qsound_p.h"
49 
51 
53 
55  : QObject(parent)
56 {
57  if (!servers)
58  servers = new QList<QAuServer*>;
59  servers->prepend(this);
60 }
61 
63 {
64  servers->removeAll(this);
65  if (servers->count() == 0) {
66  delete servers;
67  servers = 0;
68  }
69 }
70 
71 void QAuServer::play(const QString& filename)
72 {
73  QSound s(filename);
74  play(&s);
75 }
76 
78 
79 static QAuServer& server()
80 {
81  if (!servers) qt_new_audio_server();
82  return *servers->first();
83 }
84 
86 {
87 public:
88  QSoundPrivate(const QString& fname)
89  : filename(fname), bucket(0), looprem(0), looptotal(1)
90  {
91  }
92 
94  {
95  delete bucket;
96  }
97 
100  int looprem;
102 };
103 
174 void QSound::play(const QString& filename)
175 {
176  server().play(filename);
177 }
178 
190  : QObject(*new QSoundPrivate(filename), parent)
191 {
192  server().init(this);
193 }
194 
195 #ifdef QT3_SUPPORT
196 
210 QSound::QSound(const QString& filename, QObject* parent, const char* name)
211  : QObject(*new QSoundPrivate(filename), parent)
212 {
214  server().init(this);
215 }
216 #endif
217 
226 {
227  if (!isFinished())
228  stop();
229 }
230 
236 bool QSound::isFinished() const
237 {
238  Q_D(const QSound);
239  return d->looprem == 0;
240 }
241 
258 {
259  Q_D(QSound);
260  d->looprem = d->looptotal;
261  server().play(this);
262 }
263 
269 int QSound::loops() const
270 {
271  Q_D(const QSound);
272  return d->looptotal;
273 }
274 
282 {
283  Q_D(const QSound);
284  return d->looprem;
285 }
286 
301 void QSound::setLoops(int n)
302 {
303  Q_D(QSound);
304  d->looptotal = n;
305 }
306 
313 {
314  Q_D(const QSound);
315  return d->filename;
316 }
317 
327 {
328  Q_D(QSound);
329  server().stop(this);
330  d->looprem = 0;
331 }
332 
333 
348 {
349  return server().okay();
350 }
351 
357 {
358  delete s->d_func()->bucket;
359  s->d_func()->bucket = b;
360 }
361 
366 {
367  return s->d_func()->bucket;
368 }
369 
375 {
376  if (s->d_func()->looprem > 0)
377  --s->d_func()->looprem;
378  return s->d_func()->looprem;
379 }
380 
385 {
386 }
387 
389 {
390 }
401 
402 #endif // QT_NO_SOUND
double d
Definition: qnumeric_p.h:62
QAuBucket * bucket(QSound *)
Returns the internal bucket record of sound s.
Definition: qsound.cpp:365
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
~QSoundPrivate()
Definition: qsound.cpp:93
bool isFinished() const
Returns true if the sound has finished playing; otherwise returns false.
Definition: qsound.cpp:236
void setLoops(int)
Sets the sound to repeat the given number of times when it is played.
Definition: qsound.cpp:301
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual void play(const QString &filename)
Definition: qsound.cpp:71
int decLoop(QSound *)
Decrements the QSound::loopRemaining() value for sound s, returning the result.
Definition: qsound.cpp:374
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
~QSound()
Destroys this sound object.
Definition: qsound.cpp:225
int loopsRemaining() const
Returns the remaining number of times the sound will loop (this value decreases each time the sound i...
Definition: qsound.cpp:281
static bool isAvailable()
Returns true if sound facilities exist on the platform; otherwise returns false.
Definition: qsound.cpp:347
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static QList< QAuServer * > * servers
Definition: qsound.cpp:52
QSound(const QString &filename, QObject *parent=0)
Constructs a QSound object from the file specified by the given filename and with the given parent...
Definition: qsound.cpp:189
void play()
Starts playing the sound specified by this QSound object.
Definition: qsound.cpp:257
void setBucket(QSound *, QAuBucket *)
Sets the internal bucket record of sound s to b, deleting any previous setting.
Definition: qsound.cpp:356
QString filename
Definition: qsound.cpp:98
const char * name
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
QSoundPrivate(const QString &fname)
Definition: qsound.cpp:88
~QAuServer()
Definition: qsound.cpp:62
QAuServer * qt_new_audio_server()
Definition: qsound_mac.mm:181
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
virtual ~QAuBucket()
Definition: qsound.cpp:388
QAuServer(QObject *parent)
Definition: qsound.cpp:54
static QAuServer & server()
Definition: qsound.cpp:79
virtual bool okay()=0
virtual void init(QSound *)
Initializes the sound.
Definition: qsound.cpp:384
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QAuBucket * bucket
Definition: qsound.cpp:99
virtual void stop(QSound *)=0
void stop()
Stops the sound playing.
Definition: qsound.cpp:326
QString fileName() const
Returns the filename associated with this QSound object.
Definition: qsound.cpp:312
The QSound class provides access to the platform audio facilities.
Definition: qsound.h:57
int loops() const
Returns the number of times the sound will play.
Definition: qsound.cpp:269
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
int removeAll(const T &t)
Removes all occurrences of value in the list and returns the number of entries removed.
Definition: qlist.h:770