src/gui/kernel/qsound_s60.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the QtGui of the Qt Toolkit.
     7 ** This file is part of the QtGui module of the Qt Toolkit.
     8 **
     8 **
     9 ** $QT_BEGIN_LICENSE:LGPL$
     9 ** $QT_BEGIN_LICENSE:LGPL$
    10 ** No Commercial Usage
    10 ** No Commercial Usage
    11 ** This file contains pre-release code and may not be distributed.
    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
    12 ** You may use this file in accordance with the terms and conditions
    49 #include "qsound_p.h"
    49 #include "qsound_p.h"
    50 #include "qfileinfo.h"
    50 #include "qfileinfo.h"
    51 #include <private/qcore_symbian_p.h>
    51 #include <private/qcore_symbian_p.h>
    52 
    52 
    53 #include <e32std.h>
    53 #include <e32std.h>
    54 #include <MdaAudioSamplePlayer.h>
    54 #include <mdaaudiosampleplayer.h>
    55 
    55 
    56 QT_BEGIN_NAMESPACE
    56 QT_BEGIN_NAMESPACE
    57 
    57 
    58 class QAuServerS60;
    58 class QAuServerS60;
    59 
    59 
    60 class QAuBucketS60 : public QAuBucket, public MMdaAudioPlayerCallback
    60 class QAuBucketS60 : public QAuBucket, public MMdaAudioPlayerCallback
    61 {
    61 {
    62 public:
    62 public:
    63     QAuBucketS60( QAuServerS60 *server, QSound *sound);
    63     QAuBucketS60(QAuServerS60 *server, QSound *sound);
    64     ~QAuBucketS60();
    64     ~QAuBucketS60();
    65 
    65 
    66     void play();
    66     void play();
    67     void stop();
    67     void stop();
    68 
    68 
    69     inline QSound* sound() const { return m_sound; }
    69     inline QSound *sound() const { return m_sound; }
    70 
    70 
    71 public: // from MMdaAudioPlayerCallback
    71 public: // from MMdaAudioPlayerCallback
    72     void MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& aDuration);
    72     void MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& aDuration);
    73     void MapcPlayComplete(TInt aError);
    73     void MapcPlayComplete(TInt aError);
    74 
    74 
    75 private:
    75 private:
    76     QSound *m_sound;
    76     QSound *m_sound;
    77     QAuServerS60 *m_server;
    77     QAuServerS60 *m_server;
    78     bool m_prepared;
    78     bool m_prepared;
    79     bool m_playCalled;
    79     bool m_playCalled;
    80     CMdaAudioPlayerUtility* m_playUtility;
    80     CMdaAudioPlayerUtility *m_playUtility;
    81 };
    81 };
    82 
    82 
    83 
    83 
    84 class QAuServerS60 : public QAuServer
    84 class QAuServerS60 : public QAuServer
    85 {
    85 {
    86 public:
    86 public:
    87     QAuServerS60( QObject* parent );
    87     QAuServerS60(QObject *parent);
    88 
    88 
    89     void init( QSound* s )
    89     void init(QSound *s)
    90     {
    90     {
    91         QAuBucketS60 *bucket = new QAuBucketS60( this, s );
    91         QAuBucketS60 *bucket = new QAuBucketS60(this, s);
    92         setBucket( s, bucket );
    92         setBucket(s, bucket);
    93     }
    93     }
    94 
    94 
    95     void play( QSound* s )
    95     void play(QSound *s)
    96     {
    96     {
    97         bucket( s )->play();
    97         bucket(s)->play();
    98     }
    98     }
    99 
    99 
   100     void stop( QSound* s )
   100     void stop(QSound *s)
   101     {
   101     {
   102         bucket( s )->stop();
   102         bucket(s)->stop();
   103     }
   103     }
   104 
   104 
   105     bool okay() { return true; }
   105     bool okay() { return true; }
   106 
   106 
       
   107     void play(const QString& filename);
       
   108 
   107 protected:
   109 protected:
   108     void playCompleted(QAuBucketS60* bucket, int error)
   110     void playCompleted(QAuBucketS60 *bucket, int error);
   109     {
   111 
   110         QSound *sound = bucket->sound();
   112 protected:
   111         if(!error) {
   113     QAuBucketS60 *bucket(QSound *s)
   112             // We need to handle repeats by ourselves, since with Symbian API we don't
   114     {
   113             // know how many loops have been played when user asks it
   115         return (QAuBucketS60 *)QAuServer::bucket( s );
   114             if( decLoop( sound ) ) {
   116     }
   115                 play( sound );
   117 
   116             }
   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);
   117         } else {
   146         } else {
   118             // We don't have a way to inform about errors -> just decrement loops
   147             if (staticPlayingSounds.removeAll(sound))
   119             // in order that QSound::isFinished will return true;
   148                 delete sound;
   120             while(decLoop(sound)) {}
       
   121         }
   149         }
   122     }
   150     } else {
   123 
   151         // We don't have a way to inform about errors -> just decrement loops
   124 protected:
   152         // in order that QSound::isFinished will return true;
   125     QAuBucketS60* bucket( QSound *s )
   153         while (decLoop(sound)) {}
   126     {
   154         if (staticPlayingSounds.removeAll(sound))
   127         return (QAuBucketS60*)QAuServer::bucket( s );
   155             delete sound;
   128     }
   156     }
   129 
   157 }
   130     friend class QAuBucketS60;
   158 
   131 
   159 QAuServer *qt_new_audio_server()
   132 };
       
   133 
       
   134 QAuServerS60::QAuServerS60(QObject* parent) :
       
   135     QAuServer(parent)
       
   136 {
       
   137     setObjectName(QLatin1String("QAuServerS60"));
       
   138 }
       
   139 
       
   140 
       
   141 QAuServer* qt_new_audio_server()
       
   142 {
   160 {
   143     return new QAuServerS60(qApp);
   161     return new QAuServerS60(qApp);
   144 }
   162 }
   145 
   163 
   146 QAuBucketS60::QAuBucketS60( QAuServerS60 *server, QSound *sound )
   164 QAuBucketS60::QAuBucketS60(QAuServerS60 *server, QSound *sound)
   147     : m_sound( sound ), m_server( server ), m_prepared(false), m_playCalled(false)
   165     : m_sound(sound), m_server(server), m_prepared(false), m_playCalled(false)
   148 {
   166 {
   149     QString filepath = QFileInfo( m_sound->fileName() ).absoluteFilePath();
   167     QString filepath = QFileInfo(m_sound->fileName()).absoluteFilePath();
   150     filepath = QDir::toNativeSeparators(filepath);
   168     filepath = QDir::toNativeSeparators(filepath);
   151     TPtrC filepathPtr(qt_QString2TPtrC(filepath));
   169     TPtrC filepathPtr(qt_QString2TPtrC(filepath));
   152     TRAPD(err, m_playUtility = CMdaAudioPlayerUtility::NewL(*this);
   170     TRAPD(err, m_playUtility = CMdaAudioPlayerUtility::NewL(*this);
   153                m_playUtility->OpenFileL(filepathPtr));
   171                m_playUtility->OpenFileL(filepathPtr));
   154     if(err){
   172     if (err) {
   155         m_server->playCompleted(this, err);
   173         m_server->playCompleted(this, err);
   156     }
   174     }
   157 }
   175 }
   158 
   176 
   159 void QAuBucketS60::play()
   177 void QAuBucketS60::play()
   160 {
   178 {
   161     if(m_prepared) {
   179     if (m_prepared) {
   162         // OpenFileL call is completed we can start playing immediately
   180         // OpenFileL call is completed we can start playing immediately
   163         m_playUtility->Play();
   181         m_playUtility->Play();
   164     } else {
   182     } else {
   165         m_playCalled = true;
   183         m_playCalled = true;
   166     }
   184     }
   178     m_server->playCompleted(this, aError);
   196     m_server->playCompleted(this, aError);
   179 }
   197 }
   180 
   198 
   181 void QAuBucketS60::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
   199 void QAuBucketS60::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
   182 {
   200 {
   183     if(aError) {
   201     if (aError) {
   184         m_server->playCompleted(this, aError);
   202         m_server->playCompleted(this, aError);
   185     } else {
   203     } else {
   186         m_prepared = true;
   204         m_prepared = true;
   187         if(m_playCalled){
   205         if (m_playCalled){
   188             play();
   206             play();
   189         }
   207         }
   190     }
   208     }
   191 }
   209 }
   192 
   210 
   193 QAuBucketS60::~QAuBucketS60()
   211 QAuBucketS60::~QAuBucketS60()
   194 {
   212 {
   195     if(m_playUtility){
   213     if (m_playUtility){
   196         m_playUtility->Stop();
   214         m_playUtility->Stop();
   197         m_playUtility->Close();
   215         m_playUtility->Close();
   198     }
   216     }
   199 
   217 
   200     delete m_playUtility;
   218     delete m_playUtility;