javauis/nokiasound_akn/src/cmidclip.cpp
branchRCL_3
changeset 24 6c158198356e
parent 23 e5618cc85d74
child 25 ae942d28ec0e
equal deleted inserted replaced
23:e5618cc85d74 24:6c158198356e
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  This class implements wav playing.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <AudioPreference.h>
       
    21 
       
    22 #include "com_nokia_mid_sound_Sound.h"
       
    23 #include "CMIDClip.h"
       
    24 #include "MMIDEventSender.h"
       
    25 
       
    26 namespace
       
    27 {
       
    28 const TInt KMIDMaxJavaVolume = 255;
       
    29 const TInt KMIDMinNativeVolume = 1;
       
    30 const TInt KMIDHeaderLength = 4;
       
    31 _LIT8(KMIDHeaderWAV, "RIFF");
       
    32 _LIT8(KMIDHeaderAMR, "#!AM");
       
    33 const TInt KMIDClipForever = 999999;
       
    34 const TInt KMIDMinDataSize = 4;
       
    35 }
       
    36 
       
    37 CMIDClip* CMIDClip::NewL(const TDesC8* aData, MMIDEventSender* aEventSender)
       
    38 {
       
    39     CMIDClip* self = new(ELeave) CMIDClip(aEventSender);
       
    40     CleanupStack::PushL(self);
       
    41     self->ConstructL(aData);
       
    42     CleanupStack::Pop(self);
       
    43     return self;
       
    44 }
       
    45 
       
    46 CMIDClip::~CMIDClip()
       
    47 {
       
    48     if (iMdaAudioPlayerUtility)
       
    49     {
       
    50         if (iState == EPlaying)
       
    51         {
       
    52             iMdaAudioPlayerUtility->Stop();
       
    53         }
       
    54     }
       
    55     delete iMdaAudioPlayerUtility;
       
    56     delete iClip;
       
    57     delete iSchedulerWait;
       
    58 }
       
    59 
       
    60 CMIDClip::CMIDClip(MMIDEventSender* aEventSender)
       
    61         : CMIDSoundImpl(aEventSender)
       
    62 {
       
    63 }
       
    64 
       
    65 void CMIDClip::ConstructL(const TDesC8* aData)
       
    66 {
       
    67     CMIDSoundImpl::ConstructL();
       
    68     iSchedulerWait = new(ELeave) CActiveSchedulerWait;
       
    69 
       
    70     if (aData->Length() < KMIDMinDataSize)
       
    71     {
       
    72         iState = ENotSupported;
       
    73         User::Leave(KErrArgument);
       
    74     }
       
    75 
       
    76     TPtrC8 header = aData->Left(KMIDHeaderLength);
       
    77 
       
    78     if ((header != KMIDHeaderWAV()) && (header != KMIDHeaderAMR))
       
    79     {
       
    80         iState = ENotSupported;
       
    81         User::Leave(KErrArgument);
       
    82     }
       
    83 
       
    84     iState = EInitialising;
       
    85     iClip = aData->AllocL();
       
    86     iMdaAudioPlayerUtility =
       
    87         CMdaAudioPlayerUtility::
       
    88         NewDesPlayerReadOnlyL(*iClip, *this,
       
    89                               KAudioPriorityRecording);
       
    90     iMdaAudioPlayerUtility->UseSharedHeap();
       
    91     iSchedulerWait->Start();
       
    92 }
       
    93 
       
    94 TInt CMIDClip::Play(TInt aLoop)
       
    95 {
       
    96     __ASSERT_DEBUG(iState == EReadyToPlay, User::Invariant());
       
    97 
       
    98     if (aLoop == 0)
       
    99     {
       
   100         // There is no known working method to play clip forever, so we play
       
   101         // it for a long time instead.
       
   102         aLoop = KMIDClipForever;
       
   103     }
       
   104 
       
   105     --aLoop;
       
   106 
       
   107     // Setting repeats to 1 causes sound played twice
       
   108     if (aLoop > 0)
       
   109     {
       
   110         iMdaAudioPlayerUtility->SetRepeats(aLoop, TTimeIntervalMicroSeconds(0));
       
   111     }
       
   112 
       
   113     iEventSender->SendEvent(com_nokia_mid_sound_Sound_SOUND_PLAYING);
       
   114 
       
   115     iMdaAudioPlayerUtility->Play();
       
   116     iState = EPlaying;
       
   117     iPlayed = ETrue;
       
   118     return KErrNone;
       
   119 }
       
   120 
       
   121 void CMIDClip::Stop()
       
   122 {
       
   123     if (iState == EPlaying)
       
   124     {
       
   125         iEventSender->SendEvent(com_nokia_mid_sound_Sound_SOUND_STOPPED);
       
   126         iMdaAudioPlayerUtility->Stop();
       
   127         iState = EReadyToPlay;
       
   128     }
       
   129 }
       
   130 
       
   131 void CMIDClip::SetVolume(TInt aVolume)
       
   132 {
       
   133     iVolume = aVolume;
       
   134     TInt volume = 0;
       
   135     if (aVolume)
       
   136     {
       
   137         volume = (((iMdaAudioPlayerUtility->MaxVolume()
       
   138                     - KMIDMinNativeVolume + 1)
       
   139                    * aVolume)
       
   140                   / (KMIDMaxJavaVolume + 1)) + KMIDMinNativeVolume;
       
   141     }
       
   142     iMdaAudioPlayerUtility->SetVolume(volume);
       
   143 }
       
   144 
       
   145 TInt CMIDClip::Volume()
       
   146 {
       
   147     return iVolume;
       
   148 }
       
   149 
       
   150 void CMIDClip::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
       
   151 {
       
   152     if (aError == KErrNone)
       
   153     {
       
   154         //Setting iVolume to max because
       
   155         //CMdaAudioPlayerUtility doesn't have Volume()
       
   156         iVolume = KMIDMaxJavaVolume;
       
   157         iState = EReadyToPlay;
       
   158     }
       
   159     else
       
   160     {
       
   161         if (aError == KErrNotSupported)
       
   162         {
       
   163             iState = ENotSupported;
       
   164         }
       
   165         else
       
   166         {
       
   167             iState = ENotReady;
       
   168         }
       
   169     }
       
   170     iSchedulerWait->AsyncStop();
       
   171 }
       
   172 
       
   173 void CMIDClip::MapcPlayComplete(TInt /*aError*/)
       
   174 {
       
   175     iEventSender->SendEvent(com_nokia_mid_sound_Sound_SOUND_STOPPED);
       
   176     iState = EReadyToPlay;
       
   177 }
       
   178 
       
   179 void CMIDClip::Release()
       
   180 {
       
   181     if (iMdaAudioPlayerUtility)
       
   182     {
       
   183         if (iState == EPlaying)
       
   184         {
       
   185             iMdaAudioPlayerUtility->Stop();
       
   186         }
       
   187     }
       
   188     delete iMdaAudioPlayerUtility;
       
   189     iMdaAudioPlayerUtility = NULL;
       
   190     delete iClip;
       
   191     iClip = NULL;
       
   192     iState = ENotReady;
       
   193 }
       
   194 
       
   195 //End of File
       
   196