gps/GPSProject/MusicPlayer/MusicPlayerDlg.cpp

770 lines
17 KiB
C++
Executable File

// MusicPlayerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MusicPlayer.h"
#include "MusicPlayerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMusicPlayerDlg dialog
CMusicPlayerDlg::CMusicPlayerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMusicPlayerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMusicPlayerDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_InitDC = false;
// 初始化背景画刷
CBitmap tmpBmp;
// 从资源中加载背景图片
tmpBmp.LoadBitmap(IDB_MUSIC_WND);
m_BgDC.CreateCompatibleDC(NULL);
m_BgDC.SelectObject(&tmpBmp);
tmpBmp.DeleteObject();
// 加载资源文件
theApp.LoadLanguageRes(theApp.GetLanguage());
m_txtFont.CreateFont(18,0,0,0,FW_BOLD,0,0,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,_T("Arial"));
WCHAR strRes[MAX_PATH];
CString(LoadString(theApp.GetResourceLib(),IDS_MUSICPLAYER_TITLE,strRes,MAX_PATH));
m_TitleText = strRes;
m_SourceFile = L"";
m_pGraph = NULL;
m_pSeek = NULL;
m_pEvent = NULL;
m_pMediaControl = NULL;
m_InitGraph = FALSE;
m_BlockD = FALSE;
m_ReplayAll = TRUE;
m_RandomPlay = TRUE;
m_Timer = NULL;
}
CMusicPlayerDlg::~CMusicPlayerDlg()
{
m_BgDC.DeleteDC();
m_MemDC.DeleteDC();
m_BlockDC.DeleteDC();
m_BlockDCD.DeleteDC();
m_txtFont.DeleteObject();
if (m_Timer)
{
timeKillEvent(m_Timer);
}
if (m_pGraph)
{
m_pGraph->Release(); // Graph
m_pGraph = NULL;
}
if (m_pEvent)
{
m_pEvent->Release(); // 事件接口
m_pEvent = NULL;
}
if (m_pSeek)
{
m_pSeek->Release(); // 数据流定位接口
m_pSeek = NULL;
}
if (m_pMediaControl)
{
m_pMediaControl->Release(); // 播放器接口
m_pMediaControl = NULL;
}
}
void CMusicPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMusicPlayerDlg)
DDX_Control(pDX, IDC_STATIC_TIME, m_labTime);
DDX_Control(pDX, IDC_STATIC_FILENAME, m_labFile);
DDX_Control(pDX, IDC_STATIC_ALL, m_labAllTime);
DDX_Control(pDX, IDC_BUTTON_STOP, m_butStop);
DDX_Control(pDX, IDC_BUTTON_REDOM, m_butRandom);
DDX_Control(pDX, IDC_BUTTON_PRE, m_butPre);
DDX_Control(pDX, IDC_BUTTON_OPEN, m_butOpen);
DDX_Control(pDX, IDC_BUTTON_PLAY, m_butPlay);
DDX_Control(pDX, IDC_BUTTON_NEXT, m_butNext);
DDX_Control(pDX, IDC_BUTTON_EXIT, m_butExit);
DDX_Control(pDX, IDC_BUTTON_CIRCLE, m_butAll);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMusicPlayerDlg, CDialog)
//{{AFX_MSG_MAP(CMusicPlayerDlg)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_BN_CLICKED(IDC_BUTTON_CIRCLE, OnButtonCircle)
ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
ON_BN_CLICKED(IDC_BUTTON_NEXT, OnButtonNext)
ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
ON_BN_CLICKED(IDC_BUTTON_PRE, OnButtonPre)
ON_BN_CLICKED(IDC_BUTTON_REDOM, OnButtonRedom)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
ON_MESSAGE(WM_DSPLAYER,OnDSPlayer)
ON_MESSAGE(WM_DRAWPLAYTIME, OnPlayerTimer)
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMusicPlayerDlg message handlers
BOOL CMusicPlayerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
MoveWindow(0,0,320,240);
m_butExit.MoveWindow(282,6,25,25);
m_butExit.LoadBitmaps(IDB_MUSIC_CLOSE,IDB_MUSIC_CLOSED);
if (m_ReplayAll)
{
m_butAll.LoadBitmaps(IDB_MUSIC_CIRCLE);
}
else
{
m_butAll.LoadBitmaps(IDB_MUSIC_CIRCLED);
}
m_butAll.MoveWindow(2,56,46,46);
if (m_RandomPlay)
{
m_butRandom.LoadBitmaps(IDB_MUSIC_RANDOM);
}
else
{
m_butRandom.LoadBitmaps(IDB_MUSIC_RANDOMD);
}
m_butRandom.MoveWindow(2,113,46,46);
m_butOpen.MoveWindow(2,170,46,46);
m_butOpen.LoadBitmaps(IDB_MUSIC_OPEN,IDB_MUSIC_OPEND);
m_butPlay.MoveWindow(272,37,46,46);
m_butPlay.LoadBitmaps(IDB_MUSIC_PLAY,IDB_MUSIC_PLAYD,NULL,IDB_MUSIC_PLAYDIS);
m_butStop.MoveWindow(272,87,46,46);
m_butStop.LoadBitmaps(IDB_MUSIC_STOP,IDB_MUSIC_STOPD,NULL,IDB_MUSIC_STOPDIS);
m_butPre.MoveWindow(272,137,46,46);
m_butPre.LoadBitmaps(IDB_MUSIC_BACK,IDB_MUSIC_BACKD,NULL,IDB_MUSIC_BACKDIS);
m_butNext.MoveWindow(272,187,46,46);
m_butNext.LoadBitmaps(IDB_MUSIC_NEXT,IDB_MUSIC_NEXTD,NULL,IDB_MUSIC_NEXTDIS);
m_labTime.MoveWindow(87,107,99,31);
m_labTime.SetTxtFont(1);
m_labTime.SetWindowText(L"00:00/");
m_labAllTime.MoveWindow(186,113,50,22);
m_labAllTime.SetTxtFont(2);
m_labAllTime.SetWindowText(L"00:00");
m_labFile.MoveWindow(60,158,199,22);
m_labFile.SetWindowText(L"");
SetTimer(1,500,NULL);
m_Timer = timeSetEvent(1000,100,(LPTIMECALLBACK)PlayerTimeProc,(DWORD)this,TIME_PERIODIC);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMusicPlayerDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
m_MemDC.BitBlt(0,0,320,240,&m_BgDC,0,0,SRCCOPY);
m_MemDC.DrawText(m_TitleText,CRect(43,4,320,32),DT_LEFT|DT_VCENTER);
ShowProgress();
dc.BitBlt(0,0,320,240,&m_MemDC,0,0,SRCCOPY);
// Do not call CDialog::OnPaint() for painting messages
}
BOOL CMusicPlayerDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
if (!m_InitDC)
{
CBitmap tmpBmp;
tmpBmp.CreateCompatibleBitmap(pDC,320,240);
m_MemDC.CreateCompatibleDC(pDC);
m_MemDC.SelectObject(&tmpBmp);
m_MemDC.SelectObject(&m_txtFont);
m_MemDC.SetBkMode(TRANSPARENT);
m_MemDC.SetTextColor(RGB(255,255,255));
tmpBmp.DeleteObject();
tmpBmp.LoadBitmap(IDB_MUSIC_BAR);
m_BlockDC.CreateCompatibleDC(pDC);
m_BlockDC.SelectObject(&tmpBmp);
tmpBmp.DeleteObject();
tmpBmp.LoadBitmap(IDB_MUSIC_BAR);
m_BlockDCD.CreateCompatibleDC(pDC);
m_BlockDCD.SelectObject(&tmpBmp);
tmpBmp.DeleteObject();
m_InitDC = true;
}
return FALSE;
//return CDialog::OnEraseBkgnd(pDC);
}
void CMusicPlayerDlg::OnButtonCircle()
{
// TODO: Add your control notification handler code here
if (m_ReplayAll)
{
m_butAll.LoadBitmaps(IDB_MUSIC_CIRCLED);
}
else
{
m_butAll.LoadBitmaps(IDB_MUSIC_CIRCLE);
}
m_butAll.Invalidate();
m_ReplayAll = !m_ReplayAll;
}
void CMusicPlayerDlg::OnButtonExit()
{
// TODO: Add your control notification handler code here
EndDialog(IDOK);
}
void CMusicPlayerDlg::OnButtonNext()
{
// TODO: Add your control notification handler code here
m_SourceFile = g_Playlist.GetNextWorkFileName(false,m_RandomPlay);
bool bPlay = IsPlaying();
if (m_pMediaControl)
{
m_pMediaControl->Stop();
}
CreateGraph();
if (bPlay)
{
m_pMediaControl->Run();
m_butPlay.LoadBitmaps(IDB_MUSIC_PAUSE,IDB_MUSIC_PAUSED,0,0);
}
m_labFile.SetWindowText(GetFileName(m_SourceFile));
Sleep(1000);
}
void CMusicPlayerDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
CMusicOpenFileDlg openDlg;
openDlg.SetWndTitle(m_TitleText);
if (openDlg.DoModal() == IDOK)
{
m_SourceFile = openDlg.GetFilePath();
if (g_Playlist.m_TotalFiles > 1)
{
m_butNext.EnableWindow(TRUE);
m_butPre.EnableWindow(TRUE);
}
else
{
m_butNext.EnableWindow(FALSE);
m_butPre.EnableWindow(FALSE);
}
if (m_pMediaControl)
{
m_pMediaControl->Stop();
}
CreateGraph();
OnButtonPlay();
}
}
void CMusicPlayerDlg::OnButtonPlay()
{
// TODO: Add your control notification handler code here
if (m_SourceFile.IsEmpty())
{
if (m_SourceFile.IsEmpty())
{
OnButtonOpen();
}
}
else
{
if (IsPlaying())
{
if (m_pMediaControl)
{
m_butPlay.LoadBitmaps(IDB_MUSIC_PLAY,IDB_MUSIC_PLAYD);
m_pMediaControl->Pause();
}
}
else
{
if (m_pMediaControl)
{
m_butPlay.LoadBitmaps(IDB_MUSIC_PAUSE,IDB_MUSIC_PAUSED);
m_pMediaControl->Run();
}
}
}
m_labFile.SetWindowText(GetFileName(m_SourceFile));
m_butStop.EnableWindow(TRUE);
m_butPlay.Invalidate();
}
void CMusicPlayerDlg::OnButtonPre()
{
// TODO: Add your control notification handler code here
m_SourceFile = g_Playlist.GetNextWorkFileName(true,m_RandomPlay);
//MessageBox(m_SourceFile);
//m_labName.SetWindowText(m_Config.m_MusicList.sFileNames[m_Config.m_MusicList.iCurrentFile]);
bool bPlay = IsPlaying();
if (m_pMediaControl)
{
m_pMediaControl->Stop();
}
CreateGraph();
if (bPlay)
{
m_pMediaControl->Run();
m_butPlay.LoadBitmaps(IDB_MUSIC_PAUSE,IDB_MUSIC_PAUSED,0,0);
}
m_labFile.SetWindowText(GetFileName(m_SourceFile));
Sleep(1000);
}
void CMusicPlayerDlg::OnButtonRedom()
{
// TODO: Add your control notification handler code here
if (m_RandomPlay)
{
m_butRandom.LoadBitmaps(IDB_MUSIC_RANDOMD);
}
else
{
m_butRandom.LoadBitmaps(IDB_MUSIC_RANDOM);
}
m_butRandom.Invalidate();
m_RandomPlay = !m_RandomPlay;
}
void CMusicPlayerDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
if (!IsStopped())
{
m_pMediaControl->Stop();
m_butStop.EnableWindow(FALSE);
// 设置播放位置
SetCurrentPosition(0);
m_butPlay.LoadBitmaps(IDB_MUSIC_PLAY,IDB_MUSIC_PLAYD);
m_butPlay.Invalidate();
}
}
void PlayerTimeProc(UINT uTimerID,UINT uMsg,DWORD_PTR dwUser,DWORD_PTR dw1,DWORD dw2)
{
CMusicPlayerDlg* pDlg = (CMusicPlayerDlg*)dwUser;
//pDlg->SendMessage(WM_DRAWPLAYTIME,0,0);
PostMessage(pDlg->GetSafeHwnd(),WM_DRAWPLAYTIME,NULL,NULL);
}
void CMusicPlayerDlg::CreateGraph()
{
// 建立Graph
CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC,IID_IGraphBuilder,(void **)&m_pGraph);
m_pGraph->RenderFile(m_SourceFile,NULL);
m_pGraph->QueryInterface(IID_IMediaEventEx,(void **)&m_pEvent);
m_pEvent->SetNotifyWindow((OAHWND)(this->GetSafeHwnd()),WM_DSPLAYER,0);
m_pGraph->QueryInterface(IID_IMediaSeeking,(void**)&m_pSeek);
m_pSeek->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME);
m_pGraph->QueryInterface(IID_IMediaControl,(void **)&m_pMediaControl);
m_InitGraph = true;
}
bool CMusicPlayerDlg::IsPlaying()
{
// 判断当前播放器是否正在播放
if (m_pGraph && m_pMediaControl)
{
OAFilterState statePlay = State_Stopped;
if (SUCCEEDED(m_pMediaControl->GetState(10,&statePlay)))
{
return (statePlay == State_Running);
}
}
return false;
}
bool CMusicPlayerDlg::IsPaused()
{
// 判断当前播放器是否暂停
if (m_pGraph && m_pMediaControl)
{
OAFilterState statePlay = State_Stopped;
if (SUCCEEDED(m_pMediaControl->GetState(10,&statePlay)))
{
return (statePlay == State_Paused);
}
}
return false;
}
bool CMusicPlayerDlg::IsStopped()
{
// 判断当前播放器是否已经停止
if (m_pGraph && m_pMediaControl)
{
OAFilterState statePlay = State_Stopped;
if (SUCCEEDED(m_pMediaControl->GetState(10,&statePlay)))
{
return (statePlay == State_Stopped);
}
}
return false;
}
HRESULT CMusicPlayerDlg::GetPlayerStatus()
{
// 判断当前播放器状态
if (m_pGraph && m_pMediaControl)
{
OAFilterState statePlay = State_Stopped;
if (SUCCEEDED(m_pMediaControl->GetState(10,&statePlay)))
{
return statePlay;
}
}
return -1;
}
int CMusicPlayerDlg::GetDuration()
{
// 获取媒体文件总长度
__int64 lTime = -1;
if (m_pGraph && m_pSeek)
{
m_pSeek->GetDuration(&lTime);
lTime /= 10000000;
}
return (int)lTime;
}
int CMusicPlayerDlg::GetCurrentPosition()
{
// 获取媒体文件总长度
__int64 lTime = -1;
if (m_pGraph && m_pSeek)
{
m_pSeek->GetCurrentPosition(&lTime);
lTime /= 10000000;
}
return (int)lTime;
}
void CMusicPlayerDlg::DisposeGraph()
{
if (m_pMediaControl)
{
m_pMediaControl->Stop();
}
}
bool CMusicPlayerDlg::SetCurrentPosition(int iPosition)
{
// 获取媒体文件总长度
__int64 lTime = 10000000;
__int64 possion = (__int64)(iPosition * lTime);
if (m_pGraph && m_pSeek)
{
HRESULT hr = m_pSeek->SetPositions(&possion,AM_SEEKING_AbsolutePositioning |
AM_SEEKING_SeekToKeyFrame,0,AM_SEEKING_NoPositioning);
return SUCCEEDED(hr);
}
return false;
}
void CMusicPlayerDlg::OnDSPlayer()
{
long evCode,param1,param2;
HRESULT hr;
while (hr = m_pEvent->GetEvent(&evCode,&param1,&param2,0),SUCCEEDED(hr))
{
hr = m_pEvent->FreeEventParams(evCode,param1,param2);
switch(evCode)
{
case EC_COMPLETE:
// 更新按钮图片
m_butPlay.LoadBitmaps(IDB_MUSIC_PLAY,IDB_MUSIC_PLAYD,0,0);
m_butPlay.Invalidate();
m_butStop.EnableWindow(FALSE);
m_butStop.Invalidate();
// 停止播放流
m_pMediaControl->Stop();
// 复位
SetCurrentPosition(0);
m_labTime.SetWindowText(L"00:00");
if (m_ReplayAll)
{
// 更新按钮图片
m_butPlay.LoadBitmaps(IDB_MUSIC_PAUSE,IDB_MUSIC_PAUSE,0,0);
m_butPlay.Invalidate();
OnButtonNext();
}
m_pMediaControl->Run();
m_butStop.EnableWindow(TRUE);
m_butStop.Invalidate();
break;
default:
break;
}
}
}
void CMusicPlayerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch(nIDEvent)
{
case 1:
ShowProgress();
break;
default:
break;
}
CDialog::OnTimer(nIDEvent);
}
void CMusicPlayerDlg::ShowProgress()
{
// 定义设备绘图指针
CClientDC dc(this);
unsigned int len = 0;
unsigned int volLen = 0;
int startX = 61;
int startY = 189;
int volX = 193;
DWORD lVolume;
if (waveOutGetVolume(0,&lVolume) == MMSYSERR_NOERROR)
{
lVolume &= 0xFFFF;
lVolume = lVolume / 1365;
volLen = lVolume;
if (volX + volLen > 241)
{
volLen = 241 - volX;
}
}
else
{
volLen = 0;
}
if (GetDuration() > 0)
{
if(GetCurrentPosition() >= GetDuration())
{
len = 83;
}
else
{
len = (unsigned int)(83 * GetCurrentPosition()
/ GetDuration());
}
}
CDC tmpDC;
CBitmap tmpBmp;
tmpBmp.CreateCompatibleBitmap(&dc,320,240);
tmpDC.CreateCompatibleDC(&dc);
tmpDC.SelectObject(&tmpBmp);
tmpDC.BitBlt(0,0,320,240,&m_MemDC,0,0,SRCCOPY);
TransparentImage(tmpDC.m_hDC,startX + len,startY,18,17,
m_BlockDC.m_hDC,0,0,18,17,RGB(255,0,0));
TransparentImage(tmpDC.m_hDC,volX + volLen,startY,18,17,
m_BlockDCD.m_hDC,0,0,18,17,RGB(255,0,0));
dc.BitBlt(0,0,320,240,&tmpDC,0,0,SRCCOPY);
tmpDC.DeleteDC();
tmpBmp.DeleteObject();
}
void CMusicPlayerDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (point.x >= 61 && point.y >= 189 && point.x <= 161 && point.y <= 206)
{
if (!IsStopped() && m_pSeek)
{
int iCurrent = (point.x - 61) * GetDuration() / 83;
if (iCurrent > 0)
{
if (SetCurrentPosition(iCurrent))
{
ShowProgress();
}
}
}
}
else if (point.x >= 193 && point.y >= 189 && point.x <= 258 && point.y <= 206)
{
int iCurrent = 65 / (point.x - 193);
if (iCurrent > 0)
{
DWORD volume = ((0xFFFF/iCurrent) << 16);
volume &= 0xFFFF0000;
volume |= 0xFFFF/iCurrent;
waveOutSetVolume(0,volume);
}
}
CDialog::OnLButtonDown(nFlags, point);
}
CString CMusicPlayerDlg::GetFileName(CString strFileName)
{
CString FileExt;
FileExt.Empty();
// 文件名长度
int strLen = strFileName.GetLength() - 1;
for(int i = strLen; i >= 0; i--)
{
// 查找文件名中最好一个'.'
if(strFileName.GetAt(i) == '\\')
{
// 提取文件扩展名
FileExt = strFileName.Right(strLen - i);
break;
}
}
return FileExt;
}
void CMusicPlayerDlg::OnPlayerTimer(WPARAM wParam, LPARAM lParam)
{
ShowPlayTime();
}
void CMusicPlayerDlg::ShowPlayTime()
{
if (IsPaused() || IsPlaying())
{
int FileTime = GetDuration();
if(FileTime < 0)
{
FileTime = 0;
}
int tolSecond = GetCurrentPosition();
CString str;
if(tolSecond < 0)
{
tolSecond = 0;
}
unsigned char Minute = (int)(tolSecond / 60);
unsigned char Second = tolSecond % 60;
str.Format(_T("%02d:%02d"),Minute,Second);
m_labTime.SetWindowText(str);
tolSecond = FileTime;
Minute = (int)(tolSecond / 60);
Second = tolSecond % 60;
str.Format(L"/%d:%02d",Minute,Second);
m_labAllTime.SetWindowText(str);
m_labAllTime.ShowWindow(SW_SHOW);
m_labTime.ShowWindow(SW_SHOW);
}
}