Playing a WAV file
4 Mar 2003 - 10:58
Keywords :

Playing a WAV file is easier as it may look at first sight since the OS does most of the work. In this project the class CSoundPlayer implements all the necessary stuff to do this:

#include <MdaAudioSamplePlayer.h>

class CSoundPlayer: public CBase, public MMdaAudioPlayerCallback
{
public:
   static CSoundPlayer* NewL(const TDesC& aFile);
   static CSoundPlayer* NewLC(const TDesC& aFile);
   ~CSoundPlayer();
   void PlayL();
   void StopL();

   //
   // from MMdaAudioPlayerCallback
   //
   void MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& aDuration);
   void MapcPlayComplete(TInt aError);

private:
   CSoundPlayer();
   void ConstructL(const TDesC& aFile);

private:
   enum TState
   {
       ENotReady,
       EReady,
       EPlaying
   };

   TState iState;
   CMdaAudioPlayerUtility* iMdaPlayer;
};

The key classes are :
-  CMdaAudioPlayerUtility which implements the decoder. The class CSoundPlayer has a private member of this class called iMdaPlayer.
-  MMdaAudioPlayerCallback which is a kind of observer on iMdaPlayer. Basically this mixin class requires the implementation of the MapcInitComplete() and MapcPlayComplete() which will be described below.

Initialisation of the player

The player is initialised by calling CSoundPlayer::NewL(<filename>) or CSoundPlayer::NewLC(<filename>). The second-phase constructor of the CSoundPlayer object will initialise the iMdaPlayer object using CMdaAudioPlayerUtility::NewFilePlayerL():

void CSoundPlayer::ConstructL(const TDesC& aFile)
{
 //
 // Create a file audio player utility instance
 //
 iMdaPlayer=CMdaAudioPlayerUtility::NewFilePlayerL(aFile,*this);
}

A second type of constructor, CMdaAudioPlayerUtility::NewDesPlayerL() is available if your WAV sample is already in RAM memory.

The player is not ready to play yet. Actually, if you try to call iMdaPlayer->PlayL() right after the NewFilePlayerL() call, you will probably hear nothing: you have to wait the player instance constuction to be ready to play. It will be signalled to you when the callback method MapcInitComplete() is called by the framework. Two typical implementation of this function are shown below:

//
// Implementation 1: set a iState flag to ready
// to reflect the fact that the player is ready
//
void CSoundPlayer::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
{
   iState = aError ? ENotReady : EReady;
}


//
// Implementation 2: play the file immediately
//
void CSoundPlayer::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
{
   if (!aError)
       iMdaPlayer->PlayL();
}

Playing the file

Once the initialisation is complete, and as shown is the code above, the playback of the file is asked by a call to CMdaAudioPlayerUtility::PlayL(). In the Sound1 project, this is done by calling CSoundPlayer::PlayL():

void CSoundPlayer::Play()
{
   if(iState==EReady)
   {
       iState=EPlaying;
       iMdaPlayer->Play();
   }
}

The framework will notify you the end of the playback by a call to MapcPlayComplete(). Here is the implementation for Sound1:

void CSoundPlayer::MapcPlayComplete(TInt aError)
{
   iState = aError ? ENotReady : EReady;
}

 

In order to run this example, you must put a WAV file called play.wav in the directory C:\System\Apps\Sound\ of your device (C:\Symbian\6.1\Series60\Epoc32\Wins\c\system\apps\Sound under the simulator).
AttachmentSize
sound1.zip15.83 KB
Tutorial posted March 4th, 2003 by eric

Submitted by Anonymous on Sat, 2003-03-08 16:16.

HI,Eric I use the CMdaAudioPlayerUtility::NewFilePlayerL( _L("C:\\System\\Apps\\Sound\\test.wav"),*this) to open a .wav file ,but the emulator shows "System error!" The test.wav file is just in the c:\system\apps\sound directory. Why?

Submitted by Eric (not verified) on Mon, 2003-03-10 09:19.

There are several reason for the error to occur. On the emulator, the two most obvious ones are:
-  the file is not found
-  the sound data inside the WAV file has a format which is not supported ( a safe choice is to create a WAV file with 8 or 16 bit linear PCM data at 16khz).

I have another version of the player that take care of these two issues and that will be released soon.


Submitted by parisnlc (not verified) on Sat, 2003-09-27 20:40.

Hi Eric, I am clueless to why I am getting the same error as reported by others.

I have tried using the original play.wav that comes with the SDK sound sdk sample

..as well as 8bit 8hz PCM mono 8bit 16hz PCM mono 16bit 8hz PCM mono 16bit 16hz PCM mono

I have placed the wav file (for sanity check) in both the z\system\apps\SOUND\ folder (as you indicated) as well as the path specified by your CSoundPlayer::NewL(_L("C:\\System\\Apps\\Sound\\play.wav")); function call.

Unfortunately I always get the system error and can't think what is wrong.

I'm about to start checking my Windows service packs and all sorts... Any ideas?

parisnlc.


Submitted by eric (not verified) on Sat, 2003-09-27 20:53.

Humm. Never had any issue with this code. You should provide a more detailed error code (check ).

Note that "Z:" is not the path to use (use "C:" instead). And recheck all the path to your sound file (you can even add a FileExists() check in your code before CSoundPlayer::NewL() to be sure that the issue does not come from path).


Submitted by Anonymous on Sun, 2003-09-28 17:49.

problem resolved by uninstalling SDK from D:\ drive and reinstalling onto C:\

Submitted by Unico on Wed, 2007-08-15 17:40.

Hi Eric,

According to some threads in Forum Nokia if this example plays .wav on emulator, it will play .mp3 in real device if the format is supported (Yes in this case nokia 6630) without changes in code, is it correct?

Best Regards!

Note: http://discussion.forum.nokia.com/forum/showthread.php?t=69935&highlight=play


Submitted by utsav (not verified) on Mon, 2005-10-03 22:39.

Hi,

I just downloaded sound1.zip. When I run it on the emulator it does not play any file but I just see the UI framework. Could someone please tell me how to run this code and play a wave file. I'm a naive Symbian user and I would really appreciate step by step instructions. I am using CodeWarrior IDE. Thanks in advance.


Submitted by Anonymous on Wed, 2005-10-12 23:23.

Hi,

I could get this code to work with symbian 8.1a, but not with the latest one available-> 9.1 Is there anything different in the new sdk that may not work with this?


Submitted by King Kong (not verified) on Mon, 2006-04-03 15:36.

Hi,

I tried using the player, it works fine on the emulator, but when transferring a sis file onto the phone, it doesn't seem to pick up the wav files. So I am really confused, Could you please shed some light on the matter.

Thanks


Submitted by skhimsara (not verified) on Wed, 2006-07-26 22:42.

hi.. i tried using the above code and it works fine when the phone is idle.. however, when a call is on... and if i use this software, this sound is not played... how do i make it play when the call is on...?? and why isn't it working when the call is on..

Thanks in advance Sid


Submitted by Gyanesh (not verified) on Wed, 2006-09-13 11:52.

Hi I am new to symbian. I have been trying runing the sound example provided with sdk(i.e. 8.0a) with CodeWarrior.But I get an error code aError = -5, thus istate = ENotReady is chosen and I am unable to play. So I tried using the above sound example but here also the same thing occurs, while debuging I found that istate is ENotReady. I have installed sdk on c drive.I have play.wav file in C:\Symbian\8.0a\S60_2nd_FP2_CW\Epoc32\winscw\c\system\Apps\sound Does anyone know whats the problem here?

Submitted by krishnankutty85 on Fri, 2007-05-11 11:10.

i am getting the same error.i dont have a sound folder. i created one and still has a problem
Kindly suggest a solution if anyone knows


Submitted by Gyanesh (not verified) on Wed, 2006-09-13 12:55.

I changed the wave file to the one supplied with sdk exapmle, and now its working fine.So there is some difference in their wav format and the one we normally get.

Submitted by Sahdev (not verified) on Sat, 2007-02-17 13:02.

Hi All

Can someone please put some light on the DRMPlayerUtility for playing .amr in 3rd edition, as I can't find anything in detail about that either on NewLC or Forum.nokia.

Regards


Submitted by sateesh (not verified) on Wed, 2007-03-07 13:09.

Hi , I am new to symbian. I have been trying runing the sound example provided with sdk(i.e. 8.0a) with CodeWarrior.But I get an error code aError = -5, thus istate = ENotReady is chosen and I am unable to play. So I tried using the above sound example but here also the same thing occurs, while debuging I found that istate is ENotReady. I have installed sdk on c drive.I have play.wav file in C:\Symbian\8.0a\S60_2nd_FP2_CW\Epoc32\winscw\c\system\Apps\sound Does anyone know whats the problem here?

Iam also getting the same problem... u told that ur problem is solved. can u tel me the details of that error? And also one question can u tel me where we have to put the wav file also tel me whether to give the complete path? If possible send me the path which u have given in ur good.

It's very urgent so please reply me as soon as possible,

Thank u, sateesh



copyright 2003-2009 NewLC SARL