📄 musicplayerdlg.cpp
字号:
if (m_pMediaControl)
{
m_butPlay.LoadBitmaps(IDB_MUSIC_PAUSE,IDB_MUSIC_PAUSED);
m_pMediaControl->Run();
}
}
m_labFile.SetWindowText(m_SourceFileName);
m_butStop.EnableWindow(TRUE);
m_butPlay.Invalidate();
}
}
void CMusicPlayerDlg::OnButtonPre()
{
// TODO: Add your control notification handler code here
//跳过无效文件
int FauseTimes=0;
m_SourceFile=L" ";
while (GetFileAttributes(m_SourceFile) == 0xFFFFFFFF)
{
if (FauseTimes++ == g_Playlist.m_TotalFiles)
{
m_SourceFile=L" ";
m_SourceFileName=L"";
m_labFile.SetWindowText(m_SourceFileName);
g_Playlist.m_TotalFiles=0;
g_Playlist.m_CurrentFile=0;
return;
}
m_SourceFile = g_Playlist.GetNextWorkFileName(true,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_SourceFileName=m_SourceFile.Right(m_SourceFile.GetLength() -m_SourceFile.ReverseFind(L'\\')-1);
m_labFile.SetWindowText(m_SourceFileName);
InvalidateRect(CRect(60,163,258,177));
}
void CMusicPlayerDlg::OnButtonRedom()
{
// TODO: Add your control notification handler code here
m_RandomPlay = !m_RandomPlay;
if (m_RandomPlay)
{
m_butRandom.LoadBitmaps(IDB_MUSIC_RANDOMD,IDB_MUSIC_RANDOMD);
}
else
{
m_butRandom.LoadBitmaps(IDB_MUSIC_RANDOM,IDB_MUSIC_RANDOM);
}
m_butRandom.Invalidate();
}
void CMusicPlayerDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
if (!IsStopped())
{
m_pMediaControl->Stop();
m_butStop.EnableWindow(FALSE);
m_butPlay.LoadBitmaps(IDB_MUSIC_PLAY,IDB_MUSIC_PLAYD);
m_butPlay.Invalidate();
// 设置播放位置
SetCurrentPosition(0);
if ((!m_SourceFile.IsEmpty()) && (GetFileAttributes(m_SourceFile) != 0xFFFFFFFF) )
{
}
}
}
/*------------------------------------------------------------------------------*/
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);
SetCurrentPosition(0);
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 true;
}
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,¶m1,¶m2,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;
}
InvalidateRect(CRect(59,60,259,78));
//中途拔卡处理
if ((!m_SourceFile.IsEmpty()) && (GetFileAttributes(m_SourceFile) == 0xFFFFFFFF) )
{
m_SourceFile = L"";
m_SourceFileName=L"";
OnButtonStop();
m_butNext.EnableWindow(FALSE);
m_butPre.EnableWindow(FALSE);
g_Playlist.m_FileList.RemoveAll();
g_Playlist.m_TotalFiles = 0;
g_Playlist.m_CurrentFile = 0;
}
CDialog::OnTimer(nIDEvent);
}
void CMusicPlayerDlg::ShowProgress()
{
// 定义设备绘图指针
CClientDC dc(this);
unsigned int len = 0;
unsigned int volLen = 0;
int startX = 60;
int startY = 189;
//音量
int volX = 192;
DWORD lVolume;
if (waveOutGetVolume(0,&lVolume) == MMSYSERR_NOERROR)
{
lVolume &= 0xFFFF;
volLen = lVolume / (0xFFFF/51);
if (volLen > 51)
{
volLen = 51;
}
}
else
{
volLen = 0;
}
//播放进度
if (GetDuration() > 0)
{
if(GetCurrentPosition() >= GetDuration())
{
len = 86;
}
else
{
len = (unsigned int)(86 * 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::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 = L" "+str;
str.Format(L"/%d:%02d",Minute,Second);
m_labAllTime.SetWindowText(str);
m_labAllTime.ShowWindow(SW_SHOW);
m_labTime.ShowWindow(SW_SHOW);
//
}
}
BOOL CMusicPlayerDlg::UpDateRegistryFromAudio(DWORD dwVolume)
{
HKEY hKey;
LONG lResult;
DWORD dwType;
DWORD dwLen;
lResult = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("ControlPanel\\Volume"), 0, KEY_ALL_ACCESS, &hKey); //Frecall added;
if(ERROR_SUCCESS == lResult)
{
dwType = REG_DWORD;
dwLen = sizeof(DWORD);
RegSetValueEx(hKey,TEXT("Volume"),0,REG_DWORD,(LPBYTE)&dwVolume,sizeof(dwVolume));
RegFlushKey(hKey);
return TRUE;
}
else
{
return FALSE;
}
}
void CMusicPlayerDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_PickPosition=0;
m_PickVolume=0;
if (point.x >= 51 && point.y >= 189 && point.x <= 171 && point.y <= 206)
{
m_PickPosition=1;
int c = point.x-60; // 162-60-18=84
if(c > 84)
c = 84;
else if(c <0)
c = 0;
if (!IsStopped() && m_pSeek)
{
int iCurrent = c * GetDuration() / 84;
if (SetCurrentPosition(iCurrent))
{
ShowProgress();
}
}
}
//音量
else if (point.x >= 183 && point.y >= 189 && point.x <= 270 && point.y <= 206)
{
m_PickVolume=1;
int L=point.x-192; //260-192-18=50
if(L > 50)
L = 50;
else if(L <0)
L = 0;
DWORD volume = ((0xFFFF*L/50) << 16);
volume &= 0xFFFF0000;
volume |= volume>>16;
UpDateRegistryFromAudio(volume);
waveOutSetVolume(0,volume);
}
CDialog::OnLButtonDown(nFlags, point);
}
void CMusicPlayerDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_PickPosition)
{
int c = point.x-60; // 162-60-18=84
if(c > 84)
c = 84;
else if(c <0)
c = 0;
if (!IsStopped() && m_pSeek)
{
int iCurrent = c * GetDuration() / 84;
if (SetCurrentPosition(iCurrent))
{
ShowProgress();
}
}
}
if (m_PickVolume)
{
int L=point.x-192; //260-192-18=50
if(L > 50)
L = 50;
else if(L <0)
L = 0;
DWORD volume = ((0xFFFF*L/50) << 16);
volume &= 0xFFFF0000;
volume |= volume>>16;
waveOutSetVolume(0,volume);
ShowProgress();
}
CDialog::OnMouseMove(nFlags, point);
}
void CMusicPlayerDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_PickPosition=0;
if (m_PickVolume)
{ m_PickVolume=0;
DWORD volume;
waveOutGetVolume(0,&volume);
UpDateRegistryFromAudio(volume);
}
CDialog::OnLButtonUp(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -