嘿嘿,今天再次尝试了播放声音的程序,终于成功了
Player.h
#ifndef PLAYER_H_
#define PLAYER_H_
#include <MdaAudioSamplePlayer.h>
#include <mda/common/audio.h>
#include <mmf/common/mmfutilities.h>
#include <e32base.h>
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 <coecntrl.h>
#include <eiklabel.h>
#include <aknutils.h>
#include "Player.h"
#include "FirstFuckContainer.h"
CPlayer::CPlayer()
{
}
CPlayer::~CPlayer()
{
if (iPlayer)
{
delete iPlayer;
iPlayer = NULL;
}
}
void CPlayer::SetContainer(CCoeControl* aContainer)
{
iContainer = static_cast<CFirstFuckContainer*> (aContainer);
}
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<CEikLabel*>(iContainer->ComponentControl(0)); //这label只是用来显示提示而已
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<CEikLabel*> ( iContainer->ComponentControl(0) );
iLabel->SetTextL(_L("Player is playing"));
iLabel->SetExtentToWholeScreen();
iLabel->DrawDeferred();
}
}
void CPlayer::StopSound()
{
if (iState == EPlaying)
{
iPlayer->Stop();
iState = EPrepared;
CEikLabel* label = static_cast<CEikLabel*>(iContainer->ComponentControl(0));
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<CEikLabel*>(iContainer->ComponentControl(0));
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<CEikLabel*>(iContainer->ComponentControl(0));
label->SetTextL(_L("Player is finished"));
label->SetExtentToWholeScreen();
label->DrawDeferred();
}
}