util/src/gui/kernel/qsound_s60.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 QtGui module of the Qt Toolkit.
       
     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 
       
    44 #ifndef QT_NO_SOUND
       
    45 
       
    46 #include "qdir.h"
       
    47 #include "qapplication.h"
       
    48 #include "qsound.h"
       
    49 #include "qsound_p.h"
       
    50 #include "qfileinfo.h"
       
    51 #include <private/qcore_symbian_p.h>
       
    52 
       
    53 #include <e32std.h>
       
    54 #include <mdaaudiosampleplayer.h>
       
    55 
       
    56 QT_BEGIN_NAMESPACE
       
    57 
       
    58 class QAuServerS60;
       
    59 
       
    60 class QAuBucketS60 : public QAuBucket, public MMdaAudioPlayerCallback
       
    61 {
       
    62 public:
       
    63     QAuBucketS60(QAuServerS60 *server, QSound *sound);
       
    64     ~QAuBucketS60();
       
    65 
       
    66     void play();
       
    67     void stop();
       
    68 
       
    69     inline QSound *sound() const { return m_sound; }
       
    70 
       
    71 public: // from MMdaAudioPlayerCallback
       
    72     void MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& aDuration);
       
    73     void MapcPlayComplete(TInt aError);
       
    74 
       
    75 private:
       
    76     QSound *m_sound;
       
    77     QAuServerS60 *m_server;
       
    78     bool m_prepared;
       
    79     bool m_playCalled;
       
    80     CMdaAudioPlayerUtility *m_playUtility;
       
    81 };
       
    82 
       
    83 
       
    84 class QAuServerS60 : public QAuServer
       
    85 {
       
    86 public:
       
    87     QAuServerS60(QObject *parent);
       
    88 
       
    89     void init(QSound *s)
       
    90     {
       
    91         QAuBucketS60 *bucket = new QAuBucketS60(this, s);
       
    92         setBucket(s, bucket);
       
    93     }
       
    94 
       
    95     void play(QSound *s)
       
    96     {
       
    97         bucket(s)->play();
       
    98     }
       
    99 
       
   100     void stop(QSound *s)
       
   101     {
       
   102         bucket(s)->stop();
       
   103     }
       
   104 
       
   105     bool okay() { return true; }
       
   106 
       
   107     void play(const QString& filename);
       
   108 
       
   109 protected:
       
   110     void playCompleted(QAuBucketS60 *bucket, int error);
       
   111 
       
   112 protected:
       
   113     QAuBucketS60 *bucket(QSound *s)
       
   114     {
       
   115         return (QAuBucketS60 *)QAuServer::bucket( s );
       
   116     }
       
   117 
       
   118     friend class QAuBucketS60;
       
   119 
       
   120     // static QSound::play(filename) cannot be stopped, meaning that playCompleted
       
   121     // will get always called and QSound gets removed form this list.
       
   122     QList<QSound *> staticPlayingSounds;
       
   123 };
       
   124 
       
   125 QAuServerS60::QAuServerS60(QObject *parent) :
       
   126     QAuServer(parent)
       
   127 {
       
   128     setObjectName(QLatin1String("QAuServerS60"));
       
   129 }
       
   130 
       
   131 void QAuServerS60::play(const QString& filename)
       
   132 {
       
   133     QSound *s = new QSound(filename);
       
   134     staticPlayingSounds.append(s);
       
   135     play(s);
       
   136 }
       
   137 
       
   138 void QAuServerS60::playCompleted(QAuBucketS60 *bucket, int error)
       
   139 {
       
   140     QSound *sound = bucket->sound();
       
   141     if (!error) {
       
   142         // We need to handle repeats by ourselves, since with Symbian API we don't
       
   143         // know how many loops have been played when user asks it
       
   144         if (decLoop(sound)) {
       
   145             play(sound);
       
   146         } else {
       
   147             if (staticPlayingSounds.removeAll(sound))
       
   148                 delete sound;
       
   149         }
       
   150     } else {
       
   151         // We don't have a way to inform about errors -> just decrement loops
       
   152         // in order that QSound::isFinished will return true;
       
   153         while (decLoop(sound)) {}
       
   154         if (staticPlayingSounds.removeAll(sound))
       
   155             delete sound;
       
   156     }
       
   157 }
       
   158 
       
   159 QAuServer *qt_new_audio_server()
       
   160 {
       
   161     return new QAuServerS60(qApp);
       
   162 }
       
   163 
       
   164 QAuBucketS60::QAuBucketS60(QAuServerS60 *server, QSound *sound)
       
   165     : m_sound(sound), m_server(server), m_prepared(false), m_playCalled(false)
       
   166 {
       
   167     QString filepath = QFileInfo(m_sound->fileName()).absoluteFilePath();
       
   168     filepath = QDir::toNativeSeparators(filepath);
       
   169     TPtrC filepathPtr(qt_QString2TPtrC(filepath));
       
   170     TRAPD(err, m_playUtility = CMdaAudioPlayerUtility::NewL(*this);
       
   171                m_playUtility->OpenFileL(filepathPtr));
       
   172     if (err) {
       
   173         m_server->playCompleted(this, err);
       
   174     }
       
   175 }
       
   176 
       
   177 void QAuBucketS60::play()
       
   178 {
       
   179     if (m_prepared) {
       
   180         // OpenFileL call is completed we can start playing immediately
       
   181         m_playUtility->Play();
       
   182     } else {
       
   183         m_playCalled = true;
       
   184     }
       
   185 
       
   186 }
       
   187 
       
   188 void QAuBucketS60::stop()
       
   189 {
       
   190     m_playCalled = false;
       
   191     m_playUtility->Stop();
       
   192 }
       
   193 
       
   194 void QAuBucketS60::MapcPlayComplete(TInt aError)
       
   195 {
       
   196     m_server->playCompleted(this, aError);
       
   197 }
       
   198 
       
   199 void QAuBucketS60::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
       
   200 {
       
   201     if (aError) {
       
   202         m_server->playCompleted(this, aError);
       
   203     } else {
       
   204         m_prepared = true;
       
   205         if (m_playCalled){
       
   206             play();
       
   207         }
       
   208     }
       
   209 }
       
   210 
       
   211 QAuBucketS60::~QAuBucketS60()
       
   212 {
       
   213     if (m_playUtility){
       
   214         m_playUtility->Stop();
       
   215         m_playUtility->Close();
       
   216     }
       
   217 
       
   218     delete m_playUtility;
       
   219 }
       
   220 
       
   221 
       
   222 #endif // QT_NO_SOUND
       
   223 
       
   224 QT_END_NAMESPACE