Chinaunix首页 | 论坛 | 博客
  • 博客访问: 22152
  • 博文数量: 28
  • 博客积分: 670
  • 博客等级: 上士
  • 技术积分: 285
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-09 11:27
文章分类

全部博文(28)

文章存档

2011年(28)

我的朋友
最近访客

分类: 嵌入式

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 (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(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 ( 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(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(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(iContainer->ComponentControl(0));
            label->SetTextL(_L("Player is finished"));
            label->SetExtentToWholeScreen();
            label->DrawDeferred();
            }
    }

阅读(525) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~