音乐播放
此示例代码展示如何使用音乐播放组件。
初始化com组件:if (SUCCEEDED( CoCreateInstance(CLSID_PlayerCore, NULL,CLSCTX_INPROC_SERVER ,IID_MZ_SysFile,(void **)&m_pFile))) { if( !SUCCEEDED(m_pFile->QueryInterface( IID_PlayerCore_Play, (void**)&m_pPlayer))) { m_pFile->Release(); m_pFile = NULL; } }
#include "01_MzMusicPlayWnd.h" #include <UsbNotifyApi.h> #include <CallNotifyApi.h> #include <mzfc/CReg.h> #include <BackLightApi.h> #include <initguid.h> #include <IPlayerCore_IID.h> #include <PlayerCore_GUID.h> #include <IMzUnknown_IID.h> #define MZ_TIMER_ID_NO_SLEEP (MZ_TIMER_ID + 0x778B) #define WM_GRAPHNOTIFY (WM_USER + 13) #define TEST_BTN_WIDTH 120 #define TEST_BTN_HEIGHT 100 #define IDC_BASE 200 #define IDC_PLAY_BTN_ID (IDC_BASE + 1) #define IDC_PAUSE_BTN_ID (IDC_BASE + 2) #define IDC_STOP_BTN_ID (IDC_BASE + 3) #define IDC_POSTION_BAR_ID (IDC_BASE + 4) MZ_IMPLEMENT_DYNAMIC(MzMusicPlayWnd); #define DEFAULT_VOLUME 50 DWORD GetRingVolume() { CMzRegKey regKey; regKey.Open(HKEY_CURRENT_USER, TEXT("ControlPanel\\Volume")); DWORD value = DEFAULT_VOLUME; LPCTSTR valueName = TEXT("UsrVolume"); if (ERROR_SUCCESS != regKey.QueryValue(value, valueName)) { regKey.SetValue(DEFAULT_VOLUME, valueName); return DEFAULT_VOLUME; } else { return value; } } int GetUsableIdleTimeout() { static int noSleepInterval = -1; if (noSleepInterval == -1) { noSleepInterval = GetSystemIdleTimeout(); if (noSleepInterval == 0) { noSleepInterval = 9000; } else { noSleepInterval >>= 1; } if (noSleepInterval < 1000) { noSleepInterval = 1000; } } return noSleepInterval; } MzMusicPlayWnd::MzMusicPlayWnd() { m_timerActive = false; m_usbMsg = -1; m_callMsg = -1; m_smsMsg = -1; m_curFilePath[0] = 0; m_pPlayer = NULL; m_pFile = NULL; } MzMusicPlayWnd::~MzMusicPlayWnd() { _ReleasePlayer(); } BOOL MzMusicPlayWnd::OnInitDialog() { if (!CMzWndEx::OnInitDialog()) { return FALSE; } if (GetUsbConnectType() == USB_MASSSTORAGE_ATTACH) { //MzMessageBoxEx(m_hWnd, LOADDLLSTRING(IDS_STRING_MASSSTORAGE_ATTACH), LOADDLLSTRING(IDS_STRING_NOTICE)); return FALSE; } if (SUCCEEDED( CoCreateInstance(CLSID_PlayerCore, NULL,CLSCTX_INPROC_SERVER ,IID_MZ_SysFile,(void **)&m_pFile))) { if( !SUCCEEDED(m_pFile->QueryInterface( IID_PlayerCore_Play, (void**)&m_pPlayer))) { m_pFile->Release(); m_pFile = NULL; } } //其他初始化代码 int width = GetWidth(); int height = GetHeight(); int btnSpace = (width - 3 * TEST_BTN_WIDTH) / 4; int left = btnSpace; int top = height - btnSpace - TEST_BTN_HEIGHT; m_positionBar.SetPos(left, top - TEST_BTN_HEIGHT, width - 2 * btnSpace, TEST_BTN_HEIGHT); m_positionBar.SetID(IDC_POSTION_BAR_ID); m_positionBar.SetRange(0, 0); m_positionBar.SetPadpos(0); AddUiWin(&m_positionBar); m_playBtn.SetPos(left, top, TEST_BTN_WIDTH, TEST_BTN_HEIGHT); m_playBtn.SetID(IDC_PLAY_BTN_ID); m_playBtn.SetText(L"Play"); AddUiWin(&m_playBtn); left += TEST_BTN_WIDTH + btnSpace; m_pauseBtn.SetPos(left, top, TEST_BTN_WIDTH, TEST_BTN_HEIGHT); m_pauseBtn.SetID(IDC_PAUSE_BTN_ID); m_pauseBtn.SetText(L"Pause"); AddUiWin(&m_pauseBtn); left += TEST_BTN_WIDTH + btnSpace; m_stopBtn.SetPos(left, top, TEST_BTN_WIDTH, TEST_BTN_HEIGHT); m_stopBtn.SetID(IDC_STOP_BTN_ID); m_stopBtn.SetText(L"Stop"); AddUiWin(&m_stopBtn); //系统交互 m_ringVolume = GetRingVolume(); m_usbMsg = RegisterUsbNotifyMsg(); m_callMsg = GetCallRegisterMessage(); m_smsMsg = GetSmsRegisterMessage(); return TRUE; } LRESULT MzMusicPlayWnd::MzDefWndProc( UINT message, WPARAM wParam, LPARAM lParam ) { switch (message) { case WM_GRAPHNOTIFY: { if (m_pPlayer) { if (m_pPlayer->GetEventType() == EC_COMPLETE) { _StopMusic(); } } return S_OK; } break; case MZ_WM_NOTIFY_MSG_ONOFF: { if (lParam == 1) { _StopMusic(); } else { _OnCallEnd(); } return S_OK; } break; case MZ_MOUSE_RELEASED: { if (lParam > 0) { m_pPlayer->SeekTo(lParam * 1000); _OnPosition(); } return S_OK; } break; default: { if (message == m_usbMsg) { if (wParam == USB_MASSSTORAGE_ATTACH) { _OnClose(); } return S_OK; } else if (message == m_callMsg) { if (wParam == PAL_CALLSTAT_BIT_INCOMING) { _StopMusic(); } else { _OnCallEnd(); } } else if (message == m_smsMsg) { if (wParam == SMS_BEGIN) { _StopMusic(); } else { _OnCallEnd(); } } } } return CMzWndEx::MzDefWndProc( message, wParam, lParam ); } void MzMusicPlayWnd::_PlayMusic(const wchar_t *songPath) { if (!m_pPlayer) { return; } EnableWindow(m_hWnd, FALSE); if (wcscmp(m_curFilePath, songPath)) { wcscpy(m_curFilePath, songPath); m_pFile->SetName(songPath); if (SUCCEEDED(m_pPlayer->OpenFile())) { if (m_pPlayer->IsAudioOnly()) { m_pPlayer->SetNotify(m_hWnd, WM_GRAPHNOTIFY); m_pPlayer->SetVolume(m_ringVolume); m_pPlayer->EnableGetPosTimer(true); //为true时,OnTimer中才能有ID为MZ_TIMER_ID_GET_POSITION的响应,默认为false m_pPlayer->Play(); m_positionBar.SetRange(0, m_pPlayer->GetLength() / 1000); _SetSleepEnable(false); } } } else { m_pPlayer->Play(); } EnableWindow(m_hWnd, TRUE); } void MzMusicPlayWnd::_ReleasePlayer() { if (m_pFile) { m_pFile->Release(); m_pFile = NULL; } if (m_pPlayer) { m_pPlayer->Release(); m_pPlayer = NULL; } } void MzMusicPlayWnd::_SetSleepEnable(bool enable) { if (enable) { if (m_timerActive) { KillTimer(m_hWnd, MZ_TIMER_ID_NO_SLEEP); m_timerActive = false; } } else { if (!m_timerActive) { SetTimer(m_hWnd, MZ_TIMER_ID_NO_SLEEP, GetUsableIdleTimeout(), NULL); m_timerActive = true; } } } void MzMusicPlayWnd::_PauseMusic() { if (m_pPlayer) { m_pPlayer->Pause(); _SetSleepEnable(true); } } void MzMusicPlayWnd::_StopMusic() { if (m_pPlayer) { m_curFilePath[0] = 0; m_pPlayer->Stop(); m_pPlayer->Close(); _SetSleepEnable(true); _OnPosition(); } } void MzMusicPlayWnd::_OnClose() { _ReleasePlayer(); _SetSleepEnable(true); EndModal(1); } void MzMusicPlayWnd::_OnCallEnd() { } void MzMusicPlayWnd::OnTimer( UINT_PTR nIDEvent ) { switch (nIDEvent) { case MZ_TIMER_ID_NO_SLEEP: { SystemIdleTimerReset(); } break; case MZ_TIMER_ID_GET_POSITION: { _OnPosition(); } break; } } void MzMusicPlayWnd::OnMzCommand( WPARAM wParam, LPARAM lParam ) { switch (LOWORD(wParam)) { case IDC_PLAY_BTN_ID: { _PlayMusic(L"\\Windows\\RingTone\\月下江南.lnk"); } break; case IDC_PAUSE_BTN_ID: { _PauseMusic(); } break; case IDC_STOP_BTN_ID: { _StopMusic(); } break; default: break; } } void MzMusicPlayWnd::_OnPosition() { m_positionBar.SetPadpos(m_pPlayer->GetCurPos() / 1000); m_positionBar.Invalidate(); }