qtmobility/src/multimedia/qsoundeffect_qsound_p.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 //
       
    43 //  W A R N I N G
       
    44 //  -------------
       
    45 //
       
    46 // This file is not part of the Qt API. It exists purely as an
       
    47 // implementation detail. This header file may change from version to
       
    48 // version without notice, or even be removed.
       
    49 //
       
    50 // We mean it.
       
    51 //
       
    52 
       
    53 #include <QtCore/qcoreapplication.h>
       
    54 #include <QtCore/qtimer.h>
       
    55 #include <QtCore/qfile.h>
       
    56 #include <QtGui/qsound.h>
       
    57 #include <QtMultimedia/qaudioformat.h>
       
    58 #include <QDebug>
       
    59 
       
    60 #include "qmediacontent.h"
       
    61 #include "qmediaplayer.h"
       
    62 #include "qsoundeffect_p.h"
       
    63 
       
    64 #include "wavedecoder.h"
       
    65 
       
    66 #include "qsoundeffect_qsound_p.h"
       
    67 
       
    68 
       
    69 QSoundEffectPrivate::QSoundEffectPrivate(QObject* parent):
       
    70     QObject(parent),
       
    71     m_queued(false),
       
    72     m_muted(false),
       
    73     m_state(QMediaPlayer::StoppedState),
       
    74     m_status(QMediaPlayer::NoMedia),
       
    75     m_file(0),
       
    76     m_sound(0)
       
    77 {
       
    78     m_timer = new QTimer(this);
       
    79     connect(m_timer,SIGNAL(timeout()),SLOT(checkPlayTime()));
       
    80     m_media = QMediaContent();
       
    81 }
       
    82 
       
    83 QSoundEffectPrivate::~QSoundEffectPrivate()
       
    84 {
       
    85     if (m_sound) delete m_sound;
       
    86     if (m_waveDecoder) delete m_waveDecoder;
       
    87     m_file->close();
       
    88 }
       
    89 
       
    90 qint64 QSoundEffectPrivate::duration() const
       
    91 {
       
    92     if (m_waveDecoder)
       
    93         return m_waveDecoder->size();
       
    94 
       
    95     return 0;
       
    96 }
       
    97 
       
    98 int QSoundEffectPrivate::volume() const
       
    99 {
       
   100     return 100;
       
   101 }
       
   102 
       
   103 bool QSoundEffectPrivate::isMuted() const
       
   104 {
       
   105     return m_muted;
       
   106 }
       
   107 
       
   108 QMediaContent QSoundEffectPrivate::media() const
       
   109 {
       
   110     return m_media;
       
   111 }
       
   112 
       
   113 QMediaPlayer::State QSoundEffectPrivate::state() const
       
   114 {
       
   115     return m_state;
       
   116 }
       
   117 
       
   118 QMediaPlayer::MediaStatus QSoundEffectPrivate::mediaStatus() const
       
   119 {
       
   120     return m_status;
       
   121 }
       
   122 
       
   123 void QSoundEffectPrivate::play()
       
   124 {
       
   125     if (m_sound && !m_muted) {
       
   126         m_queued = false;
       
   127         m_timer->start(20);
       
   128         m_playbackTime.start();
       
   129         m_sound->play();
       
   130         emit stateChanged(m_state = QMediaPlayer::PlayingState);
       
   131     } else if (m_status == QMediaPlayer::LoadingMedia)
       
   132         m_queued = true;
       
   133 }
       
   134 
       
   135 void QSoundEffectPrivate::stop()
       
   136 {
       
   137     m_timer->stop();
       
   138 
       
   139     if (m_sound) {
       
   140         m_sound->stop();
       
   141         emit stateChanged(m_state = QMediaPlayer::StoppedState);
       
   142     }
       
   143 }
       
   144 
       
   145 void QSoundEffectPrivate::setVolume(int volume)
       
   146 {
       
   147     Q_UNUSED(volume)
       
   148 }
       
   149 
       
   150 void QSoundEffectPrivate::setMuted(bool muted)
       
   151 {
       
   152     m_muted = muted;
       
   153 }
       
   154 
       
   155 void QSoundEffectPrivate::setMedia(const QMediaContent &media)
       
   156 {
       
   157     m_queued = false;
       
   158 
       
   159     if (media.isNull() || media.canonicalUrl().scheme() != "file") {
       
   160         m_media = QMediaContent();
       
   161         return;
       
   162     }
       
   163     if (m_media == media)
       
   164         return;
       
   165 
       
   166     m_media = media;
       
   167     m_file = new QFile(m_media.canonicalUrl().toLocalFile());
       
   168     m_file->open(QIODevice::ReadOnly|QIODevice::Unbuffered);
       
   169 
       
   170     unloadSample();
       
   171     loadSample();
       
   172 
       
   173     emit mediaChanged(m_media);
       
   174 }
       
   175 
       
   176 void QSoundEffectPrivate::decoderReady()
       
   177 {
       
   178     m_file->close();
       
   179     m_sound = new QSound(m_media.canonicalUrl().toLocalFile());
       
   180     emit mediaStatusChanged(m_status = QMediaPlayer::LoadedMedia);
       
   181 
       
   182     if (m_queued)
       
   183         play();
       
   184 }
       
   185 
       
   186 void QSoundEffectPrivate::decoderError()
       
   187 {
       
   188     m_file->close();
       
   189     emit mediaStatusChanged(m_status = QMediaPlayer::InvalidMedia);
       
   190 }
       
   191 
       
   192 void QSoundEffectPrivate::checkPlayTime()
       
   193 {
       
   194     if (m_sound->isFinished()) {
       
   195         m_timer->stop();
       
   196         m_state = QMediaPlayer::StoppedState;
       
   197         emit stateChanged(m_state);
       
   198     }
       
   199 }
       
   200 
       
   201 void QSoundEffectPrivate::loadSample()
       
   202 {
       
   203     m_waveDecoder = new WaveDecoder(m_file);
       
   204     connect(m_waveDecoder, SIGNAL(formatKnown()), SLOT(decoderReady()));
       
   205     connect(m_waveDecoder, SIGNAL(invalidFormat()), SLOT(decoderError()));
       
   206 
       
   207     m_status = QMediaPlayer::LoadingMedia;
       
   208     emit mediaStatusChanged(m_status);
       
   209 }
       
   210 
       
   211 void QSoundEffectPrivate::unloadSample()
       
   212 {
       
   213     if (m_sound == 0)
       
   214         return;
       
   215 
       
   216     m_status = QMediaPlayer::NoMedia;
       
   217 
       
   218     if (m_sound)
       
   219         delete m_sound;
       
   220 
       
   221     m_sound = 0;
       
   222 }
       
   223