分类: 嵌入式
2011-01-29 12:38:33
嘿嘿,今天再次尝试了播放声音的程序,终于成功了,注意,这个把音频文件放在模拟器的目录下
Player.h
#ifndef PLAYER_H_
#define PLAYER_H_
#include
#include
#include
#include
class CFirstFuckContainer;
class CPlayer : public MMdaAudioPlayerCallback , public CBase
{
enum TMyState
{
EInitialising,
EPlaying,
EPrepared
};
public:
void CreatePlayerL( const TDesC &aFileName );
void PlaySound();
void StopSound();
void PauseSound();
~CPlayer();
void MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds &aDuration);
void MapcPlayComplete(TInt aError);
void SetContainer(CCoeControl* aContainer);
CPlayer();
private:
CMdaAudioPlayerUtility* iPlayer;
TMyState iState;
CFirstFuckContainer *iContainer;
};
#endif /*PLAYER_H_*/
Player.cpp
#include
#include
#include
#include "Player.h"
#include "FirstFuckContainer.h"
CPlayer::CPlayer()
{
}
CPlayer::~CPlayer()
{
if (iPlayer)
{
delete iPlayer;
iPlayer = NULL;
}
}
void CPlayer::SetContainer(CCoeControl* aContainer)
{
iContainer = static_cast
}
void CPlayer::CreatePlayerL( const TDesC &aFileName)
{
TFileName fileName(aFileName);
User::LeaveIfError( CompleteWithAppPath( fileName ));
iPlayer = CMdaAudioPlayerUtility::NewFilePlayerL( fileName, *this);
iState = EInitialising;
}
void CPlayer::PauseSound()
{
if (iState == EPlaying)
{
if (iPlayer->Pause() == KErrNone)
{
iState = EPrepared;
CEikLabel* label = static_cast
label->SetTextL(_L("Player is paused"));
label->SetExtentToWholeScreen();
label->DrawDeferred();
}
}
}
void CPlayer::PlaySound()
{
if ( iState == EPrepared )
{
iPlayer->SetVolume( iPlayer->MaxVolume() );
iPlayer->Play();
iState = EPlaying;
CEikLabel *iLabel;
iLabel = static_cast
iLabel->SetTextL(_L("Player is playing"));
iLabel->SetExtentToWholeScreen();
iLabel->DrawDeferred();
}
}
void CPlayer::StopSound()
{
if (iState == EPlaying)
{
iPlayer->Stop();
iState = EPrepared;
CEikLabel* label = static_cast
label->SetTextL(_L("Player is stoped"));
label->SetExtentToWholeScreen();
label->DrawDeferred();
}
}
void CPlayer::MapcInitComplete( TInt aError, const TTimeIntervalMicroSeconds &aDuration)
{//在这里加载声音文件
if (aError == KErrNone)
{
iState = EPrepared;
CEikLabel* label = static_cast
label->SetTextL(_L("Player is ready"));
label->SetExtentToWholeScreen();
label->DrawDeferred();
//PlaySound();
}
}
void CPlayer::MapcPlayComplete( TInt aError )
{
if (aError == KErrNone)
{
iState = EPrepared;
CEikLabel* label = static_cast
label->SetTextL(_L("Player is finished"));
label->SetExtentToWholeScreen();
label->DrawDeferred();
}
}