Playing audio files
来源:互联网 发布:知乎slu圣路易斯大学 编辑:程序博客网 时间:2024/05/22 05:10
AudioPlayer.cpp
#include <Mda/Common/Resource.h>
#include <BAUTILS.H>
CAudioPlayer* CAudioPlayer::NewL(MExamplePlayStateObserver& aObserver)
{
CAudioPlayer* self = CAudioPlayer::NewLC(aObserver);
CleanupStack::Pop(self);
return self;
}
CAudioPlayer* CAudioPlayer::NewLC(MExamplePlayStateObserver& aObserver)
{
CAudioPlayer* self = new (ELeave) CAudioPlayer(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CAudioPlayer::CAudioPlayer(MExamplePlayStateObserver& aObserver)
:iObserver(aObserver),iVolume(5)
{
}
CAudioPlayer::~CAudioPlayer()
{
delete iExampleTimer;
if(iPlayerUtility)
{
iPlayerUtility->Stop();
iPlayerUtility->Close();
}
delete iPlayerUtility;
}
void CAudioPlayer::ConstructL()
{
iExampleTimer = CExampleTimer::NewL(CActive::EPriorityStandard,*this);
ReportStateAndTime();
}
void CAudioPlayer::TimerExpired(TAny* /*aTimer*/,TInt aError)
{
// update states first.
ReportStateAndTime();
if(iExampleTimer && aError != KErrCancel)
{
iExampleTimer->After(KReFreshTimeOut);
}
}
void CAudioPlayer::PlayL(const TDesC& aFileName)
{
iCurrentFile.iName.Copy(aFileName);
if(iExampleTimer)
{
iExampleTimer->Cancel();
}
if(iPlayerUtility)
{
iPlayerUtility->Stop(); // stop any play/rec
iPlayerUtility->Close();// close previously opened file.
}
delete iPlayerUtility; // and then we can delete it.
iPlayerUtility = NULL;
iPlayerUtility = CMdaAudioRecorderUtility::NewL(*this);
iPlayerUtility->OpenFileL(iCurrentFile.iName);
if(iExampleTimer)
{
iExampleTimer->After(KReFreshTimeOut);
}
}
void CAudioPlayer::StopL(void)
{
if(iExampleTimer)
{
iExampleTimer->Cancel();
}
if(iPlayerUtility)
{
iPlayerUtility->Stop();
}
ReportStateAndTime();
}
void CAudioPlayer::SetVolume(TInt& aVolume)
{
if(aVolume < 1)
aVolume = 1;
else if(aVolume > 10)
aVolume = 10;
iVolume = aVolume;// save to internal value always
if(iPlayerUtility) // and if utility exists, set it to it as well.
{
TInt Vol = ((iPlayerUtility->MaxVolume() * iVolume) / 10);
iPlayerUtility->SetVolume(Vol);
}
}
void CAudioPlayer::ReportStateAndTime(void)
{
TInt CurrPosition(0),FileDuration(0);
CMdaAudioClipUtility::TState CurrState(CMdaAudioClipUtility::ENotReady);
if(iPlayerUtility)
{
CurrState = iPlayerUtility->State();
TInt64 HelpPos = iPlayerUtility->Position().Int64();
// micro seconds, thus lets make it seconds for UIs
CurrPosition = HelpPos / 1000000;
// and with playing its the file duration.
HelpPos = iPlayerUtility->Duration().Int64();
FileDuration = HelpPos / 1000000;
}
// update valus to the UI
iObserver.StateUpdate(CurrState,CurrPosition,FileDuration);
}
void CAudioPlayer::MoscoStateChangeEvent(CBase* aObject, TInt aPreviousState, TInt aCurrentState, TInt /*aErrorCode*/)
{
if(aObject == iPlayerUtility)
{
ReportStateAndTime();
switch(aCurrentState)
{
case CMdaAudioClipUtility::EOpen:
{
// only when first opening files.
// also called with aCurrentState == EOpen, when
// playing or recording finishes.
if(aPreviousState == CMdaAudioClipUtility::ENotReady)
{
TInt Vol = ((iVolume * iPlayerUtility->MaxVolume()) / 10);
iPlayerUtility->SetVolume(Vol);
TRAPD(err,iPlayerUtility->PlayL(););
}
}
break;
case CMdaAudioClipUtility::EPlaying:
case CMdaAudioClipUtility::ERecording:
case CMdaAudioClipUtility::ENotReady:
default: // no need to do anything on these states.
break;
}
}
}
AudioPlayer.h
#include <MdaAudioSampleEditor.h>
#include <Mda/Client/Utility.h>
#include "CExampleTimer.h"
const TInt KReFreshTimeOut = 1000000; // re-fresh every second
//
class MExamplePlayStateObserver
{
public:
virtual void StateUpdate(CMdaAudioClipUtility::TState aState, TInt aPosition, TInt aDuration)=0;
};
class CAudioPlayer : public CBase, public MMdaObjectStateChangeObserver,MExampleTimerNotify
{
public:
static CAudioPlayer* NewL(MExamplePlayStateObserver& aObserver);
static CAudioPlayer* NewLC(MExamplePlayStateObserver& aObserver);
~CAudioPlayer();
public: // public functions
void PlayL(const TDesC& aFileName);
void StopL(void);
void SetVolume(TInt& aVolume);
protected: // from MMdaObjectStateChangeObserver & MExampleTimerNotify
void MoscoStateChangeEvent(CBase* aObject, TInt aPreviousState, TInt aCurrentState, TInt aErrorCode);
void TimerExpired(TAny* aTimer,TInt aError);
private:// interna functions
void ReportStateAndTime(void);
void ConstructL();
CAudioPlayer(MExamplePlayStateObserver& aObserver);
private:
MExamplePlayStateObserver& iObserver;
CMdaAudioRecorderUtility* iPlayerUtility;
TInt iVolume;
TMdaFileClipLocation iCurrentFile;
CExampleTimer* iExampleTimer;
};
PlayUtility.cpp
#include <MdaAudioTonePlayer.h>
#include <eikmenup.h>
CPlayerUtility* CPlayerUtility::NewL(const TDesC& aFileName)
{
CPlayerUtility* self = NewLC(aFileName);
CleanupStack::Pop(self);
return self;
}
CPlayerUtility* CPlayerUtility::NewLC(const TDesC& aFileName)
{
CPlayerUtility* self = new (ELeave) CPlayerUtility();
CleanupStack::PushL(self);
self->ConstructL(aFileName);
return self;
}
CPlayerUtility::~CPlayerUtility()
{
if(iPlayUtility)
{
iPlayUtility->Stop();
iPlayUtility->Close();
}
delete iPlayUtility;
}
CPlayerUtility::CPlayerUtility()
{
}
void CPlayerUtility::ConstructL(const TDesC& aFileName)
{
iPlayUtility = CMdaAudioPlayerUtility::NewFilePlayerL(aFileName, *this);
iPlaying = iPrepared = EFalse;
}
void CPlayerUtility::Play()
{
iPlayUtility->Play();
iPlaying = ETrue;
}
void CPlayerUtility::Stop()
{
iPlayUtility->Stop();
iPlaying = EFalse;
}
void CPlayerUtility::MapcPlayComplete(TInt /*aError*/)
{
iPlaying = EFalse;
}
void CPlayerUtility::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
{
if(aError == KErrNone)
{
iPrepared = ETrue;
iPlayUtility->SetVolume(iPlayUtility->MaxVolume());
iPlayUtility->Play();
}
}
PlayUtility.h
#include <e32std.h>
#include <MdaAudioSamplePlayer.h>
class CPlayerUtility : public CBase, public MMdaAudioPlayerCallback
{
public:
static CPlayerUtility* NewL(const TDesC& aFileName);
static CPlayerUtility* NewLC(const TDesC& aFileName);
~CPlayerUtility();
private:
CPlayerUtility();
void ConstructL(const TDesC& aFileName);
public:
void Play();
void Stop();
public: // from MMdaAudioToneObserver
void MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& aDuration);
void MapcPlayComplete(TInt aError);
private:
CMdaAudioPlayerUtility* iPlayUtility;
TBool iPlaying,iPrepared;
};
- Playing audio files
- symbian 如何播放声音文件Playing audio files
- Playing .wav files using C#
- Playing AVI Files In OpenGL
- Audio Processing: Playing with volume level
- PCM Audio and Wave Files
- PCM Audio and Wave Files
- Playing Audio in the Background(在后台播放音乐)
- OpenAL Tutorial - Playing WAV files (No ALUT required!)
- PCM Audio and Wave Files 2
- Play audio Files (播放音频文件)
- Product Attachments module allows downloading files, manuals, media files, audio files and other downloads in
- Loading and Playing Audio in an Applet:在applet程序中加载和播放音乐
- Loading and Playing Audio in an Application:在程序加载并播放一段音频文件
- Gaplessly playing large audio file with MS wave I/O functions
- Uploading audio, video or image files from Android to server
- Converting Audio (mp3 etc) files to m4a with AVAssetExportSession
- audio
- request中的参数不能取得
- scrathbox 代理设置
- 认识ASP.NET配置文件Web.config
- QTRunner
- 在Oracle的SQL*PLUS中启用AutoTrace查看SQL执行计划和统计信息
- Playing audio files
- 剖析ASP.NET2.0站点导航功能之建立导航
- 脏数据
- ASP.NET验证控件详解
- 什么是驱动程序
- Oracle分析表和索引(analyze)
- 模糊逻辑系统框架
- 软件开发的问题点
- Android 1.5 平台整合 Google Apps (Market;GTalk;Google Maps;GMail;Voice Search)