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;
}
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.
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.
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).
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
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.
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?
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
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
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
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
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