⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audioplayerengine.cpp

📁 一个视频播放器的源代码!供新手学习!功能很简单
💻 CPP
字号:
/**
 *
 * @brief Definition of CAudioPlayerEngine
 *
 * Copyright (c) EMCC Software Ltd 2003
 * @version 1.0
 */

#include "AudioPlayerEngine.h"

#include <eikenv.h>
#include <stringloader.h>
#include <barsread.h>
#include <aknutils.h>

#include <AudioPlayer.rsg>
#include "AudioPlayer.loc"

_LIT(KToPlayFileWav,	"\\system\\apps\\AudioPlayer\\Ring.wav");
_LIT(KToPlayFileMidi,	"\\system\\apps\\AudioPlayer\\Fur-Elise.mid");
_LIT(KToPlayFileStream,	"\\system\\apps\\AudioPlayer\\Stream.pcm");

const TInt KToneFrequency = 3000;
const TInt KToneDuration = 4000000;
const TInt KToneVolumeDenominator = 2;

/**
* C++ constructor
*/
CAudioPlayerEngine::CAudioPlayerEngine(MAudioPlayerEngineObserver& aObserver)
:iObserver(aObserver),
 iState(EStopped)
	{
	}

/**
* Destructor.
*/
CAudioPlayerEngine::~CAudioPlayerEngine()
	{
	Stop();
	}

/**
 * Symbian OS 2 phase constructor.
 * Constructs the CAudioPlayerEngine popping
 * the constructed object from the CleanupStack before returning it.
 *
 * @param aRect The rectangle for this window
 * @return The newly constructed CAudioPlayerAppView
 */
CAudioPlayerEngine* CAudioPlayerEngine::NewL(MAudioPlayerEngineObserver& aObserver)
	{
	CAudioPlayerEngine* self = new (ELeave) CAudioPlayerEngine(aObserver);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
	}

/**
* Symbian 2nd phase constructor.
* Empty implementation
*/
void CAudioPlayerEngine::ConstructL()
	{
	}

/**
* MMdaAudioToneObserver derivation
* This has been called as a result of NewL() being called on the tone player
* the tone utility is now ready for playing
*
* @param aError Error code to inform us if preparation has successfully completed
*
*/
void CAudioPlayerEngine::MatoPrepareComplete(TInt aError)
	{
	if (aError)
		{
		Stop();
		}
	else
		{
		iPlayerTone->SetVolume(iPlayerTone->MaxVolume() / KToneVolumeDenominator);
		iPlayerTone->Play();
		}
	}

/**
* MMdaAudioToneObserver derivation
* This is called when tone playing completes and informs the
* MAudioPlayerEngineObserver playing has finished.
*
* @param aError Error code to inform us if playing has successfully completed
*/
void CAudioPlayerEngine::MatoPlayComplete(TInt /*aError*/)
	{
	iState = EStopped;
	delete iPlayerTone;
	iPlayerTone = NULL;

	TRAPD(err, iObserver.HandlePlayingStoppedL());
	}

/**
* MMdaAudioPlayerCallback derivation
* This has been called as a result of NewFilePlayerL() being called on the file player
* the player utility is now ready for playing
*
* @param aError Status of the audio sample after initialization
* @param aDuration Duration of the audio sample
*/
void CAudioPlayerEngine::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
	{
	if (aError == KErrNone)
		{
		iPlayerFile->SetVolume(iPlayerFile->MaxVolume() / KToneVolumeDenominator);
		iPlayerFile->Play();
		}
	else
		{
		Stop();
		}
	}

/**
* MMdaAudioPlayerCallback derivation
* This has been called as a result of playing being completed
*
* @param aError Error code to inform if playing has successfully completed
*/
void CAudioPlayerEngine::MapcPlayComplete(TInt /*aError*/)
	{
	iState = EStopped;
	delete iPlayerFile;
	iPlayerFile = NULL;

	TRAPD(err, iObserver.HandlePlayingStoppedL());
	}

/**
* MMdaAudioOutputStreamCallback derivation
* This has been called as a result of NewL() being called on the stream player
* in order to play the stream, we must 1st write the stream buffer to the client
* once this has been done, the stream will begin playing automatically
*
* @param aError Error code to indicate if open succeeded
*/
void CAudioPlayerEngine::MaoscOpenComplete(TInt aError)
	{
	if (aError == KErrNone)
		{
		TRAPD(err, iPlayerStream->WriteL(*iStreamBuffer));
		}
	else
		{
		Stop();
		}
	}

/**
* This has been called in response to WriteL() being called on the stream player
* or in response to Stop() being called on the stream player. in the latter case,
* aError is equal to KErrAbort.
* Otherwise, we add iStreamBuffer to the client queue, so that it will be repeated,
* when the last buffer has finished
*
* @param aError   Error code to indicate if copy was successful
* @param aBuffer  A reference to the descriptor that has been copied to the server
*/
void CAudioPlayerEngine::MaoscBufferCopied(TInt aError, const TDesC8& /*aBuffer*/)
	{
	if (aError == KErrNone)
		{
		TRAPD(err, iPlayerStream->WriteL(*iStreamBuffer))
		iPlayerStream->SetVolume(iPlayerStream->MaxVolume() / KToneVolumeDenominator);
		}
	}

/**
* Callback when playback finishes
*
* @param aError Error code indicates why playback was terminated
*/
void CAudioPlayerEngine::MaoscPlayComplete(TInt /*aError*/)
	{
	iState = EStopped;
	delete iPlayerStream;
	iPlayerStream = NULL;
	delete iStreamBuffer;
	iStreamBuffer = NULL;
	delete [] iStreamData;
	iStreamData = NULL;

	TRAPD(err, iObserver.HandlePlayingStoppedL());
	}

//PLAYER FUNCTIONS

/**
* Constructs the tone player;
* This will lead to a call to MapcInitComplete()
*/
void CAudioPlayerEngine::PlayToneL()
	{
	iState = EPlaying;
	iPlayerTone = CMdaAudioToneUtility::NewL(*this);
	iPlayerTone->PrepareToPlayTone(KToneFrequency, TTimeIntervalMicroSeconds(KToneDuration));
	}

/**
* Constructs the file player;
* This will lead to a call to MapcInitComplete()
*/
void CAudioPlayerEngine::PlayWavL()
	{
	TFileName wavFile(KToPlayFileWav);
	User::LeaveIfError(CompleteWithAppPath(wavFile));
	iState = EPlaying;
	iPlayerFile = CMdaAudioPlayerUtility::NewFilePlayerL(wavFile, *this);
	}

/**
* Constructs the file player;
* This will lead to a call to MapcInitComplete()
* Note: playing midi files can only be supported on device; it is not supported under WINS
*/
void CAudioPlayerEngine::PlayMidiL()
	{
#ifndef __WINS__
	TFileName midiFile(KToPlayFileMidi);
	User::LeaveIfError(CompleteWithAppPath(midiFile));
	iState = EPlaying;
	iPlayerFile = CMdaAudioPlayerUtility::NewFilePlayerL(midiFile, *this);
#endif
	}
/**
* Constructs the stream player
* This will lead to a call to MaoscOpenComplete()
*/
void CAudioPlayerEngine::PlayStreamL()
    {
	// open the file and load it into the buffers
	RFs fs;
    CleanupClosePushL(fs);				// PUSH
	User::LeaveIfError(fs.Connect());
    RFile file;
    CleanupClosePushL(file);			// PUSH

	TFileName streamFile(KToPlayFileStream);
	User::LeaveIfError(CompleteWithAppPath(streamFile));
	User::LeaveIfError(file.Open(fs, streamFile, EFileRead | EFileShareReadersOnly));

	TInt fileSize = 0;
	file.Size(fileSize);
	iStreamData = new (ELeave) TUint8[fileSize];
    iStreamBuffer = new (ELeave) TPtr8(iStreamData, fileSize, fileSize);
	file.Read(*iStreamBuffer);

	CleanupStack::PopAndDestroy(2);		// file & fs

	iState = EPlaying;
	iPlayerStream = CMdaAudioOutputStream::NewL(*this);
	iPlayerStream->Open(&iStreamSettings);
	}

/**
* Stops whatever is playing and deletes the player
*/
void CAudioPlayerEngine::Stop()
	{
	if (iPlayerTone)
		{
		iPlayerTone->CancelPlay();
		}

	if (iPlayerFile)
		{
		iPlayerFile->Stop();
		}

	if (iPlayerStream)
		{
		iPlayerStream->Stop();
		}

	delete iPlayerTone;
	iPlayerTone = NULL;

	delete iPlayerFile;
	iPlayerFile = NULL;

	delete iPlayerStream;
	iPlayerStream = NULL;
	delete iStreamBuffer;
	iStreamBuffer = NULL;
	delete [] iStreamData;
	iStreamData = NULL;

	iState = EStopped;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -