📄 cmediabookmarker.cpp
字号:
}
return 0;
}
// OnPlay: Starts playback.
LRESULT CMediaBookmarker::OnPlay(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
m_spWMPPlayer->Play();
return 0;
}
// OnPause: Pauses playback.
LRESULT CMediaBookmarker::OnPause(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
m_spWMPPlayer->Pause();
return 0;
}
// OnStop: Stops playback.
LRESULT CMediaBookmarker::OnStop(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
m_spWMPPlayer->Stop();
return 0;
}
// OnFastForward: Sets the current media position forward by five seconds.
LRESULT CMediaBookmarker::OnFastForward(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
double dCurrentPosition;
double dDuration;
if (SUCCEEDED(m_spWMPPlayer->get_CurrentPosition(&dCurrentPosition)))
{
if (SUCCEEDED(m_spWMPPlayer->get_Duration(&dDuration)) && dCurrentPosition+5 < dDuration)
{
m_spWMPPlayer->put_CurrentPosition(dCurrentPosition + 5);
}
}
return 0;
}
// OnFastReverse: Sets the current media position back by five seconds.
LRESULT CMediaBookmarker::OnFastReverse(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
double dCurrentPosition;
double dDuration;
if (SUCCEEDED(m_spWMPPlayer->get_CurrentPosition(&dCurrentPosition)))
{
if (SUCCEEDED(m_spWMPPlayer->get_Duration(&dDuration)) && dCurrentPosition-5 < dDuration)
{
m_spWMPPlayer->put_CurrentPosition(dCurrentPosition - 5);
}
}
return 0;
}
// PlayMedia: Opens the specified media file and plays it from the specified position.
BOOL CMediaBookmarker::PlayMedia(TCHAR *szFile, double dPosition)
{
TCHAR* szFilename;
const int BACKSLASH = TEXT('\\');
int iPathLength;
int iFilenameLength;
// Stop the Player (in case it is paused).
if (FAILED(m_spWMPPlayer->Stop()))
{
return FALSE;
}
// Turn off AutoStart so the position can be set before the media plays.
if (FAILED(m_spWMPPlayer->put_AutoStart(FALSE)))
{
return FALSE;
}
// Set the file name.
if (FAILED(m_spWMPPlayer->put_FileName(szFile)))
{
return FALSE;
}
// Set the current position.
if (FAILED(m_spWMPPlayer->put_CurrentPosition(dPosition)))
{
return FALSE;
}
// Play the file.
if (FAILED(m_spWMPPlayer->Play()))
{
return FALSE;
}
// Store the current file name without the path information.
if (NULL != m_szCurrentFile)
{
delete[] m_szCurrentFile;
m_szCurrentFile = NULL;
}
m_szCurrentFile = new TCHAR[MAX_MARKLENGTH];
if (NULL != m_szCurrentFile)
{
// Find the last backslash. (NOTE: if the sample is modified
// to use URLs, search for the last forward slash as well.)
szFilename = _tcsrchr(szFile, BACKSLASH);
if (NULL == szFilename)
{
szFilename = szFile;
}
else
{
szFilename = szFilename + 1;
}
// Calculate the length of the directory path portion of the full file name.
iPathLength = szFilename - szFile;
// Calculate the file name length (including the terminating NULL).
iFilenameLength = _tcslen(szFile) - iPathLength + 1;
if (MAX_MARKLENGTH < iFilenameLength)
{
iFilenameLength = MAX_MARKLENGTH - 1;
szFilename[iFilenameLength] = TEXT('\0');
}
_tcsncpy(m_szCurrentFile, szFilename, iFilenameLength);
}
return TRUE;
}
// Bookmark: Stores the current file name and position and
// returns a formatted string for the bookmark list.
BOOL CMediaBookmarker::Bookmark(TCHAR *szBookmarkText)
{
int seconds;
int minutes;
int hours;
WCHAR *szFileName = new WCHAR[MAX_MARKLENGTH]; // Temporary storage for the file name
// Confirm that the allocation succeeded.
if (NULL == szFileName)
{
return FALSE;
}
// Retrieve the current file name and make sure it isn't empty.
if (FAILED( m_spWMPPlayer->get_FileName(&szFileName) ) || !szFileName[0])
{
delete[] szFileName;
szFileName = NULL;
return FALSE;
}
// Make sure the current slot in the file name array is empty before filling it.
if (NULL != m_bstrFileName[m_cMarkCount])
{
SysFreeString(m_bstrFileName[m_cMarkCount]);
m_bstrFileName[m_cMarkCount] = NULL;
}
// Store the file name in the file name array for future use.
m_bstrFileName[m_cMarkCount] = SysAllocString(szFileName);
delete[] szFileName;
szFileName = NULL;
// Retrieve the current position, and store it in the position array for future use.
if (FAILED( m_spWMPPlayer->get_CurrentPosition(&m_dMarkPosition[m_cMarkCount]) ))
{
return FALSE;
}
// Convert the current position (in seconds) to hours, minutes, and seconds for display.
seconds = (int)m_dMarkPosition[m_cMarkCount];
hours = seconds/60/60;
minutes = (seconds/60) % 60;
seconds = seconds % 60;
// If the current position is an hour or more from the beginning, include an hours display.
if (hours)
{
_sntprintf(szBookmarkText, MAX_MARKLENGTH, L"%02d:%02d:%02d - %s",
hours, minutes, seconds, m_szCurrentFile);
}
// If the current position is less than an hour from the beginning, exclude the hours display.
else
{
_sntprintf(szBookmarkText, MAX_MARKLENGTH, L"%02d:%02d - %s",
minutes, seconds, m_szCurrentFile);
}
return TRUE;
}
// Invoke: redirects Windows Media Player control events to the appropriate event handler.
HRESULT CMediaBookmarker::Invoke( DISPID dispIdMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS FAR *pDispParams,
VARIANT FAR *pVarResult,
EXCEPINFO FAR *pExcepInfo,
unsigned int FAR *puArgErr )
{
if (!pDispParams)
{
return E_POINTER;
}
if (pDispParams->cNamedArgs != 0)
{
return DISP_E_NONAMEDARGS;
}
HRESULT hr = S_OK;
switch (dispIdMember)
{
case IWMPEVENTS_DISPID_PLAYSTATECHANGE: OnPlayStateChange(pDispParams->rgvarg[0].lVal);
break;
default:
hr = DISP_E_MEMBERNOTFOUND;
}
return( hr );
}
// OnPlayStateChange: Called when the control changes PlayState.
void CMediaBookmarker::OnPlayStateChange(long NewState)
{
if (0 == NewState) // stopped
{
// Hide the video window and resize the bookmark list.
::MoveWindow(GetDlgItem(IDL_BOOKMARKS), 0, 40, 240, 214, FALSE);
::MoveWindow(GetDlgItem(IDC_PLAYER), 0, 254, 240, 14, FALSE);
}
else if (2 == NewState) // playing
{
// Retrieve the video height. If it is greater than zero,
// resize the video window and bookmark list.
long lPlayerHeight = 0L;
if (SUCCEEDED(m_spWMPPlayer->get_ImageSourceHeight(&lPlayerHeight)) && lPlayerHeight)
{
// Make sure the status bar stays visible.
lPlayerHeight += STATUS_BAR_HEIGHT;
// Make sure there is room for a few bookmark list entries.
if (lPlayerHeight > MAX_PLAYER_HEIGHT)
{
lPlayerHeight = MAX_PLAYER_HEIGHT;
}
::MoveWindow(GetDlgItem(IDL_BOOKMARKS), 0, 40, 240, 268 - (lPlayerHeight+40), FALSE);
::MoveWindow(GetDlgItem(IDC_PLAYER), 0, 268 - lPlayerHeight, 240, lPlayerHeight, FALSE);
}
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -