lspo816
Conqueror
Rank: 3Rank: 3



UID 9
Digest Posts 0
Credits 242
Posts 3
Money 10
Reading Access 30
Registered 27-3-2007
Status Offline
Post at 27-3-2007 16:04  Profile | P.M. 
播放WAV文件

播放一个wav文件要比想象中容易很多,因为很多事情都交给了系统。在这个工程中CSoundPlayer实现了播放wav文件的必要步骤

#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();

   //
   // 来自 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;
};

关键类:
CMdaAudioPlayerUtility : 实现了解码,CSoundPlayer有一个私有的CMdaAudioPlayerUtility类型的成员iMdaPlayer.
MMdaAudioPlayerCallback: 是iMdaPlayer的观察者(用于观察播放器中发生的时间,比如初始化结束和播放结束等)。这个类最少要实现MapcInitComplete() 和MapcPlayComplete() ,我们下面讲要讲到。

初始化播放器
CSoundPlayer::NewL() 或者 CSoundPlayer::NewLC()用于来初始化播放器,二阶构造函数将调用CMdaAudioPlayerUtility::NewFilePlayerL()来初始化iMdaPlayer .

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

还有一种构造函数,CMdaAudioPlayerUtility::NewDesPlayerL() 用于wav已经在ram内存中的情况

到现在为止,播放器还不能开始播放。实际上,如果你尝试在NewFilePlayerL()后立刻调用iMdaPlayer->PlayL() ,可能你会听不到任何东西,因为只有播放器实例构造完全结束以后才可以播放。
播放器初始话结束以后,框架会调用MapcInitComplete()。两种常见的实现方式如下:

//
// 方法 1: 设置iState设置为准备状态,通知播放器已经准备好了
//
void CSoundPlayer::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
{
   iState = aError ? ENotReady : EReady;
}



//
// 方法 2: 立刻播放文件
//
void CSoundPlayer::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
{
   if (!aError)
       iMdaPlayer->PlayL();
}

播放文件
一旦初始化结束,就像上面的例子一样,回调函数中调用了CMdaAudioPlayerUtility::PlayL()。在Sound1 例子中,这里使用的是CSoundPlayer::PlayL()

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

播放结束后,框架将调用MapcPlayComplete(),下面Sound1的实现部分

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

如果要想运行这个例子,你必须把一个play.wav放在C:\System\Apps\Sound\下  (模拟器放在C:\Symbian\6.1\Series60\Epoc32\Wins\c\system\apps\Sound )


下载源码

[ Last edited by  lspo816 at 27-3-2007 16:36 ]
Top
lspo816
Conqueror
Rank: 3Rank: 3



UID 9
Digest Posts 0
Credits 242
Posts 3
Money 10
Reading Access 30
Registered 27-3-2007
Status Offline
Post at 27-3-2007 16:05  Profile | P.M. 
Playing a WAV file
Posted on 4 March 2003 by Eric Bustarret


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() or CSoundPlayer::NewLC(). 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).

[ Last edited by  lspo816 at 27-3-2007 16:38 ]
Top
 


All times are GMT+8, the time now is 7-9-2010 03:39

CopyRight © Symbianx.cn 2007 Powered By Discuz! 5
Clear Cookies - Contact Us - Symbian OS系统[S60,UIQ]开发中文翻译论坛 - Archiver

本站原文版权归原文作者所有,本站译文版权归本站所有,如需转载请注明原文和译文出处,否则追究法律责任