Symbian学习笔记20 之 播放声音

 嘿嘿,今天再次尝试了播放声音的程序,终于成功了


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值