qtmobility/src/multimedia/qsoundeffect.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qmediacontent.h"
       
    43 #include "qmediaplayer.h"
       
    44 
       
    45 #include "qsoundeffect_p.h"
       
    46 
       
    47 #if defined(QT_MULTIMEDIA_PULSEAUDIO)
       
    48 #include "qsoundeffect_pulse_p.h"
       
    49 #elif(QT_MULTIMEDIA_QMEDIAPLAYER)
       
    50 #include "qsoundeffect_qmedia_p.h"
       
    51 #else
       
    52 #include "qsoundeffect_qsound_p.h"
       
    53 #endif
       
    54 
       
    55 QML_DEFINE_TYPE(Qt,4,6,SoundEffect,QSoundEffect)
       
    56 
       
    57 /*!
       
    58     \qmlclass QSoundEffect
       
    59     \brief The SoundEffect element provides a way to play sound effects in qml.
       
    60 
       
    61     The following example plays a wav file on mouse click.
       
    62 
       
    63     \qml
       
    64     SoundEffect {
       
    65         id: playSound
       
    66         source: "test.wav"
       
    67     }
       
    68     MouseRegion {
       
    69         id: playArea
       
    70         anchors.fill: parent
       
    71         onPressed: {
       
    72             playSound.play()
       
    73         }
       
    74     }
       
    75     \endeml
       
    76 
       
    77     \sa SoundEffect
       
    78 */
       
    79 
       
    80 /*!
       
    81     \qmlproperty QUrl SoundEffect::source
       
    82 
       
    83     This property provides a way to control the sound to play.
       
    84 */
       
    85 
       
    86 /*!
       
    87     \qmlproperty int SoundEffect::loopCount
       
    88 
       
    89     This property provides a way to control the number of times to repeat the sound on each play().
       
    90 */
       
    91 
       
    92 /*!
       
    93     \qmlproperty int SoundEffect::volume
       
    94 
       
    95     This property provides a way to control the volume for playback.
       
    96 */
       
    97 
       
    98 /*!
       
    99     \qmlproperty bool SoundEffect::muted
       
   100 
       
   101     This property provides a way to control muting.
       
   102 */
       
   103 
       
   104 /*!
       
   105     \qmlproperty int SoundEffect::duration
       
   106 
       
   107     This property holds the duration in milliseconds of the current source audio.
       
   108 */
       
   109 
       
   110 /*!
       
   111     \qmlsignal SoundEffect::sourceChanged()
       
   112 
       
   113     This handler is called when the source has changed.
       
   114 */
       
   115 
       
   116 /*!
       
   117     \qmlsignal SoundEffect::loopCountChanged()
       
   118 
       
   119     This handler is called when the number of loops has changes.
       
   120 */
       
   121 
       
   122 /*!
       
   123     \qmlsignal SoundEffect::volumeChanged()
       
   124 
       
   125     This handler is called when the volume has changed.
       
   126 */
       
   127 
       
   128 /*!
       
   129     \qmlsignal SoundEffect::mutedChanged()
       
   130 
       
   131     This handler is called when the mute state has changed.
       
   132 */
       
   133 
       
   134 /*!
       
   135     \qmlsignal SoundEffect::durationChanged()
       
   136 
       
   137     This handler is called when the duration has changed.
       
   138 */
       
   139 
       
   140 QSoundEffect::QSoundEffect(QObject *parent) :
       
   141     QObject(parent),
       
   142     m_loopCount(1),
       
   143     m_volume(100),
       
   144     m_muted(false),
       
   145     m_runningCount(0)
       
   146 {
       
   147     d = new QSoundEffectPrivate(this);
       
   148     connect(d, SIGNAL(volumeChanged(int)), SIGNAL(volumeChanged()));
       
   149     connect(d, SIGNAL(mutedChanged(bool)), SIGNAL(mutedChanged()));
       
   150     connect(d, SIGNAL(durationChanged(qint64)), SIGNAL(durationChanged()));
       
   151     connect(d, SIGNAL(stateChanged(QMediaPlayer::State)), SLOT(repeat()));
       
   152 }
       
   153 
       
   154 QSoundEffect::~QSoundEffect()
       
   155 {
       
   156     delete d;
       
   157 }
       
   158 
       
   159 QUrl QSoundEffect::source() const
       
   160 {
       
   161     return d != 0 ? d->media().canonicalUrl() : QUrl();
       
   162 }
       
   163 
       
   164 void QSoundEffect::setSource(const QUrl &url)
       
   165 {
       
   166     if (d != 0 && d->media().canonicalUrl() == url)
       
   167         return;
       
   168 
       
   169     d->setVolume(m_volume);
       
   170     d->setMuted(m_muted);
       
   171     d->setMedia(url);
       
   172 
       
   173     if (url.isEmpty())
       
   174         return;
       
   175 
       
   176     emit sourceChanged();
       
   177 }
       
   178 
       
   179 int QSoundEffect::loopCount() const
       
   180 {
       
   181     return m_loopCount;
       
   182 }
       
   183 
       
   184 void QSoundEffect::setLoopCount(int loopCount)
       
   185 {
       
   186     if (m_loopCount == loopCount)
       
   187         return;
       
   188 
       
   189     m_loopCount = loopCount;
       
   190     emit loopCountChanged();
       
   191 }
       
   192 
       
   193 int QSoundEffect::volume() const
       
   194 {
       
   195     return d != 0 ? d->volume() : m_volume;
       
   196 }
       
   197 
       
   198 void QSoundEffect::setVolume(int volume)
       
   199 {
       
   200     if (m_volume == volume)
       
   201         return;
       
   202 
       
   203     m_volume = volume;
       
   204     if (d != 0)
       
   205         d->setVolume(volume);
       
   206     else
       
   207         emit volumeChanged();
       
   208 }
       
   209 
       
   210 bool QSoundEffect::isMuted() const
       
   211 {
       
   212     return d !=  0 ? d->isMuted() : m_muted;
       
   213 }
       
   214 
       
   215 void QSoundEffect::setMuted(bool muted)
       
   216 {
       
   217     if (m_muted == muted)
       
   218         return;
       
   219 
       
   220     m_muted = muted;
       
   221     if (d != 0)
       
   222         d->setMuted(muted);
       
   223     else
       
   224         emit mutedChanged();
       
   225 }
       
   226 
       
   227 int QSoundEffect::duration() const
       
   228 {
       
   229     return d != 0 ? d->duration() : 0;
       
   230 }
       
   231 
       
   232 void QSoundEffect::play()
       
   233 {
       
   234     m_runningCount = 0;
       
   235 
       
   236     if (d != 0)
       
   237         d->play();
       
   238 }
       
   239 
       
   240 void QSoundEffect::stop()
       
   241 {
       
   242     if (d != 0)
       
   243         d->stop();
       
   244 }
       
   245 
       
   246 void QSoundEffect::repeat()
       
   247 {
       
   248     if (d->state() == QMediaPlayer::StoppedState) {
       
   249         if (++m_runningCount < m_loopCount)
       
   250             d->play();
       
   251     }
       
   252 }
       
   253