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

📄 symbian4container.cpp

📁 S60 3版 音乐播放器及进度条实现 功能:前后倒放歌曲及暂停等功能
💻 CPP
字号:
/*
============================================================================
Name        : CSymbian4Container from Symbian3Container.h
Author      : 
Version     :
Copyright   : Your copyright notice
Description : Container control implementation
============================================================================
*/

// INCLUDE FILES
#include "Symbian4Container.h"
#include <barsread.h>  //for TResourceReader
#include <eiklabel.h>  // for example label control
#include <eikprogi.h> // CEikProgressInfo
#include <Symbian4.rsg>
#include "Symbian4AppUi.h"
// ================= MEMBER FUNCTIONS =======================

// ---------------------------------------------------------
// CSymbian4Container::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
CSymbian4Container::CSymbian4Container()
{
    ibool=EFalse;
	iPBar = NULL;
	iReDraw = EFalse;
	iLabel=NULL;
	iToDoLabel=NULL;
	iNumView=NULL;
	iNameView=NULL;
}
void CSymbian4Container::ConstructL(const TRect& aRect)
{
	CreateWindowL();


	iNameView = new (ELeave) CEikLabel;//歌曲名
	iNameView->SetContainerWindowL(*this);
	{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC(reader, R_POP_MENU_CONTAINER_LABEL2 );
		iNameView->ConstructFromResourceL(reader);
		CleanupStack::PopAndDestroy(); // reader internal state
	}



	iLabel = new (ELeave) CEikLabel;
	iLabel->SetContainerWindowL( *this );
	TResourceReader reader;
	iEikonEnv->CreateResourceReaderLC(reader, R_POP_MENU_CONTAINER_LABEL5 );
	iLabel->ConstructFromResourceL(reader);
	CleanupStack::PopAndDestroy(); 


	iToDoLabel = new (ELeave) CEikLabel;
	iToDoLabel->SetContainerWindowL( *this );
	//	 iToDoLabel->SetTextL( _L(""));//_L("Add Your controls\n here") );
	TResourceReader reader1;
	iEikonEnv->CreateResourceReaderLC(reader1, R_POP_MENU_CONTAINER_LABEL6 );
	iToDoLabel->ConstructFromResourceL(reader1);
	CleanupStack::PopAndDestroy(); 

	iNumView = new (ELeave) CEikLabel;
	iNumView->SetContainerWindowL(*this);
	{
		TResourceReader reader2;
		iEikonEnv->CreateResourceReaderLC(reader2, R_POP_MENU_CONTAINER_LABEL1 );
		iNumView->ConstructFromResourceL(reader2);
		CleanupStack::PopAndDestroy(); 
	}



	MakeProcessBar();
	iMyStone = CMyStone::NewL();

	GetInfo();//获得播放文件信息

	//	iMyStone->PLayMusic(iIndex);
	//	iMyStone->Time(iTime);

	SetRect(aRect);
	ActivateL();
}

// Destructor
CSymbian4Container::~CSymbian4Container()
{
	delete iLabel;
	iLabel=NULL;
	delete iToDoLabel;
	iToDoLabel=NULL;

	delete iNumView;
	iNumView=NULL;

	delete iNameView;
	iNameView=NULL;

	delete iPBar;
	iPBar = NULL;
	if (iPeriodicTimer) //定时器停止
	{
		iPeriodicTimer->Cancel();
	}
	delete iPeriodicTimer;

	iMyStone->Stop();
	delete iMyStone;  
}

// ---------------------------------------------------------
// CSymbian4Container::SizeChanged()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
void CSymbian4Container::SizeChanged()
{
	// TODO: Add here control resize code etc.
	iNumView->SetExtent(TPoint(120,150), TSize(50, 40));
	iLabel->SetExtent( TPoint(20, 235), /*iLabel->MinimumSize()*/ TSize(90,40) );
	iToDoLabel->SetExtent( TPoint(215, 235), iToDoLabel->MinimumSize() );
	iPBar->SetExtent( TPoint(40,100), iPBar->MinimumSize() );
	iNameView->SetExtent( TPoint(60, 60), TSize(240, 40));
}
     
// ---------------------------------------------------------
// CSymbian4Container::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CSymbian4Container::CountComponentControls() const
{
	return 5; // return nbr of controls inside this container
}

// ---------------------------------------------------------
// CSymbian4Container::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CSymbian4Container::ComponentControl(TInt aIndex) const
{
	switch ( aIndex )
	{
	case 0:
		return iPBar;
	case 1:
		return iToDoLabel;
	case 2:
		return iLabel;
	case 3:
		return iNumView;
	case 4:
		return iNameView;
	default:
		return NULL;
	}
}

// ---------------------------------------------------------
// CSymbian4Container::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CSymbian4Container::Draw(const TRect& aRect) const
{
	CWindowGc& gc = SystemGc();
	// TODO: Add your drawing code here
	// example code...
	gc.SetPenStyle( CGraphicsContext::ENullPen );
	gc.SetBrushColor( KRgbGray );
	gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
	gc.DrawRect( aRect );

	iLabel->SetTextL(iStartTime);
	iToDoLabel->SetTextL(iTime);
	iNumView->SetTextL(iNumofsong);
	iNameView->SetTextL(iNameofsong);
}

// ---------------------------------------------------------
// CSymbian4Container::HandleControlEventL(
//     CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CSymbian4Container::HandleControlEventL(
	CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
{
	// TODO: Add your control event handler code here
}


void CSymbian4Container::StartTimer()
{
	
	if (iPeriodicTimer->IsActive())
	{
		iPeriodicTimer->Cancel();
	}
	iPeriodicTimer->Start(1000000, 500000, TCallBack(
		CSymbian4Container::Period, this));
   
}

TInt CSymbian4Container::Period(TAny* aPtr)
{
	((CSymbian4Container*) aPtr)->DoStep();
	return FALSE;
}

void CSymbian4Container::DoStep()
{
		if (iReDraw)
		{

			iMyStone->Time(iTime,iStartTime);
      
			slow=iMyStone->GetNum();
			ilength=iMyStone->GetLength();

			iPBar->SetAndDraw(slow);
			iPBar->SetFinalValue(ilength);
            GetInfo();
			DrawNow();
		}
		else
		{
			//iPBar->IncrementAndDraw(0);
//			iMyStone->Time(iTime,iStartTime);
			DrawNow();
		}
}

void CSymbian4Container::MakeProcessBar() //构建进度条
{
	TResourceReader reader;
	iCoeEnv->CreateResourceReaderLC(reader, R_PROGRESS_BAR );
	iPBar = new (ELeave) CEikProgressInfo;
	iPBar->ConstructFromResourceL(reader);
	CleanupStack::PopAndDestroy();
	iPBar->SetPosition(TPoint(10,40));
	iPBar->SetContainerWindowL(*this);


	iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
	StartTimer();
}

void CSymbian4Container::ReStartProgressBar()
{

	if (iPeriodicTimer) //定时器停止
	{
		iPeriodicTimer->Cancel();
	}
	delete iPeriodicTimer;
	delete iPBar;
	iPeriodicTimer = NULL;
	iPBar = NULL;
	MakeProcessBar();
	iReDraw = ETrue;
	StartTimer();
}

void CSymbian4Container::GetInfo()
{
	CSymbian4AppUi* pApp = (CSymbian4AppUi*) CEikonEnv::Static()->AppUi();
	iArrayFlat = pApp->GetPlayListPoint(); //获得播放列表
	iIndex = pApp->GetCurrent(); //获得当前播放的序号

	if(iArrayFlat->Count() == 0)//当没有歌曲时直接返回
		return;

	/*_LIT(KFormat,"%d/%d");
	iNumofsong.Zero();
	iNumofsong.AppendFormat(KFormat, iIndex + 1, iArrayFlat->Count());*/
	else{
		iNameofsong.Copy((*iArrayFlat)[iIndex].Mid(2));
		_LIT(KFormat,"%d/%d");
		iNumofsong.Zero();
		iNumofsong.AppendFormat(KFormat, iIndex + 1, iArrayFlat->Count());
		if (iIndex == iArrayFlat->Count() - 1)
			iNextsong.Copy((*iArrayFlat)[0].Mid(2));
		else
			iNextsong.Copy((*iArrayFlat)[iIndex + 1].Mid(2));
	} 
	
}

TKeyResponse  CSymbian4Container::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType )
{
	CSymbian4AppUi* pApp = (CSymbian4AppUi*) CEikonEnv::Static()->AppUi();
	TRect rc;

	if ((aType == EEventKeyDown) && (aKeyEvent.iScanCode == 14)) //leftdown
	{
		iFlag = 1;
		rc.SetRect(TPoint(131, 54), TSize(16, 16));
		DrawNow(rc);
		if (State() == 1)//1表示正在播放 2表示暂停
		{
			if (iIndex > 0)
			{
				iMyStone->Stop();
				iMyStone->PLayMusic(--iIndex);
			pApp->SetCurrent(iIndex);
			iPBar->IncrementAndDraw(-240);
		
			}
			else
				return;
		}
		if (State() == 2 && iIndex > 0)//当为暂停
		{
			iMyStone->Stop();
			pApp->SetCurrent(iIndex);

			iPBar->IncrementAndDraw(-240);//进度条返回到开始位置
		}
		if (State() == 0)
			pApp->SetCurrent(--iIndex);
		else 
			return;
       
	}
	if ((aType == EEventKeyUp) && (aKeyEvent.iScanCode == 14)) //leftup
	{
		iFlag = 2;
	}
	//右键响应处理
	if ((aType == EEventKeyDown) && (aKeyEvent.iScanCode == 15)) //rightdown
	{
		iFlag = 3;
		if (State() == 1)//1表示正在播放 2表示暂停
		{
			
			if (iIndex < iArrayFlat->Count() - 1)
				
			{
			iMyStone->Stop();
			iMyStone->PLayMusic(++iIndex);
			pApp->SetCurrent(iIndex);
		
			iPBar->IncrementAndDraw(-240);
			}
			else
				return;
		}
		if (State() == 2 && iIndex < iArrayFlat->Count())
		{
	             	iMyStone->Stop();
	             	pApp->SetCurrent(iIndex);
///////////////////////////////////////////////////////////////////////////
			iPBar->IncrementAndDraw(-240);
		}
		if (State() == 0)
			pApp->SetCurrent(++iIndex);

	}
	
	if ((aType == EEventKeyDown) && (aKeyEvent.iScanCode == 167)) //middle
	{
		if (iReDraw)
		{
			iReDraw=EFalse;
			iMyStone->Pause();

		}
		else
		{
			iReDraw=ETrue;

			iMyStone->PLayMusic(iIndex);
       
		}

		return CCoeControl::OfferKeyEventL(aKeyEvent, aType);
	}
   
}

⌨️ 快捷键说明

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