// CPlayerDlg.cpp : 实现文件 // #include "stdafx.h" #include "CPlayerDlg.h" #include "afxdialogex.h" #define FULLSCREEN_TIMERID 8 #define INTERVAL_TIMERID 9 // CPlayerDlg 对话框 IMPLEMENT_DYNAMIC(CPlayerDlg, CDialogEx) BEGIN_EVENTSINK_MAP(CPlayerDlg, CDialogEx) ON_EVENT(CPlayerDlg, IDC_OCX1, 5101, CPlayerDlg::PlayStateChangeOcx1, VTS_I4) END_EVENTSINK_MAP() // 默认构造函数 CPlayerDlg::CPlayerDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CPlayerDlg::IDD, pParent), m_player(NULL), m_bMax(false), m_bInit(false), m_bTimer(false), m_hMonitor(NULL), m_nPlayCnt(0), m_nMediaInx(0), m_pConfig(NULL) { m_hIcon = AfxGetApp()->LoadIcon(IDI_PLAYER); } // 音视频播放 CPlayerDlg::CPlayerDlg(CWmpPlayConfig *pConfig, CWnd *pParent/* = NULL*/) : CDialogEx(CPlayerDlg::IDD, pParent), m_player(NULL), m_bMax(false), m_bInit(false), m_bTimer(false), m_hMonitor(NULL), m_nPlayCnt(0), m_nMediaInx(0) { m_hIcon = AfxGetApp()->LoadIcon(IDI_PLAYER); m_pConfig = pConfig; } CPlayerDlg::~CPlayerDlg() { if (m_player != NULL) { delete m_player; m_player = NULL; } m_pConfig = NULL; } BOOL CPlayerDlg::OnInitDialog() { CDialogEx::OnInitDialog(); SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 long ScreenWidth = GetSystemMetrics(SM_CXSCREEN); //主屏幕宽度 long ScreenHeight = GetSystemMetrics(SM_CYSCREEN); //主屏幕高度 MONITORINFOEX mix; mix.cbSize = sizeof(mix); if (m_pConfig->bPrimMonitor == true) // 默认情况,在主屏显示 { m_hMonitor = MonitorFromWindow(this->GetSafeHwnd(), MONITOR_DEFAULTTONULL); GetMonitorInfo(m_hMonitor, (LPMONITORINFO)&mix); SetRect(&m_monitorRect, mix.rcMonitor.left, mix.rcMonitor.top, mix.rcMonitor.right, mix.rcMonitor.bottom); } else // 在副屏显示 { POINT pt; pt.x = ScreenWidth+5; pt.y = 5; m_hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL); //尝试获取副屏的句柄 if (m_hMonitor != NULL) { GetMonitorInfo(m_hMonitor, (LPMONITORINFO)&mix); SetRect(&m_monitorRect, mix.rcMonitor.left, mix.rcMonitor.top, mix.rcMonitor.right, mix.rcMonitor.bottom); } else { OnCancel(); // add by ly SetEvent(m_pConfig->h_WMPCreateEvent); // 发出窗口创建完成事件 return FALSE; } } CRect Rect; this->GetClientRect(&Rect); m_player = new CWMPPlayer4; /*HCURSOR hCursor = ::LoadCursor(NULL, IDC_ARROW); m_player->Create(AfxRegisterWndClass((CS_HREDRAW|CS_VREDRAW|CS_PARENTDC), hCursor), NULL, WS_VISIBLE|WS_CHILD, Rect, this, IDC_OCX1);*/ BOOL bCreateRet = m_player->Create(_T("SalesVideo"),WS_CHILD|WS_VISIBLE,Rect,this,IDC_OCX1); if (bCreateRet == FALSE) { OnCancel(); // add by ly SetEvent(m_pConfig->h_WMPCreateEvent); // 发出窗口创建完成事件 return FALSE; } // add by ly SetEvent(m_pConfig->h_WMPCreateEvent); // 发出窗口创建完成事件 m_bInit = true; ::SetWindowPos(m_player->GetSafeHwnd(),HWND_TOP,m_monitorRect.left,m_monitorRect.top,0,0,SWP_HIDEWINDOW);//HWND_TOP m_player->put_enableContextMenu(FALSE); m_player->put_stretchToFit(TRUE); m_player->put_fullScreen(FALSE);//禁止全屏 if (_stricmp(m_pConfig->strRootPath, "")) { if(m_pConfig->eMode == SINGLE) // 单一模式播放 { ::SetWindowPos(this->GetSafeHwnd(),HWND_TOP,m_monitorRect.left,m_monitorRect.top,0,0,SWP_HIDEWINDOW);//HWND_TOP m_player->put_URL(m_pConfig->strRootPath); } else if (m_pConfig->eMode == SALESRECORD || m_pConfig->eMode == THRIDSALESRECORD) // 客户经理录像模式播放 { if (SALESRECORD == m_pConfig->eMode) { ::SetWindowPos(this->GetSafeHwnd(),HWND_TOP,m_monitorRect.left,m_monitorRect.top,0,0,SWP_HIDEWINDOW);//HWND_TOP } else{ if (m_pConfig->nWndWidth > m_monitorRect.Width()) { m_pConfig->nWndWidth = m_monitorRect.Width(); } if (m_pConfig->nWndHeight > m_monitorRect.Height()) { m_pConfig->nWndHeight = m_monitorRect.Height(); } ::SetWindowPos(this->GetSafeHwnd(), HWND_TOPMOST, m_monitorRect.left+m_pConfig->nWndX, m_monitorRect.top+m_pConfig->nWndY,m_pConfig->nWndWidth,m_pConfig->nWndHeight,SWP_HIDEWINDOW);//HWND_TOP } SetTimer(FULLSCREEN_TIMERID, GetDoubleClickTime(), 0); // 开启Timer监视,防止WMP本身的全屏 m_bTimer = true; m_settings = static_cast(m_player->get_settings()); m_settings.put_autoStart(FALSE); m_playlist = m_player->get_currentPlaylist(); if (strstr(m_pConfig->strNamePrefix, "S")) { m_playlist.put_name(_T("现场销售录音录像")); } else if (strstr(m_pConfig->strNamePrefix, "W")) { m_playlist.put_name(_T("二次见证录音录像")); } else if (strstr(m_pConfig->strNamePrefix, "OFL")) // edit by ly 20180224 { m_playlist.put_name(_T("离线录音录像")); } CString videoPath(""); for(int i = 0; i != m_pConfig->nFileCnt; ++i) { videoPath.Format(_T("%s%s_%d.wmv"), m_pConfig->strRootPath, m_pConfig->strNamePrefix, i); m_playlist.appendItem(m_player->newMedia(videoPath)); } m_control = static_cast(m_player->get_controls()); m_control.play(); } else if (m_pConfig->eMode == LOCALAUDIO) // 本地音频模式播放 { ::SetWindowPos(this->GetSafeHwnd(),HWND_TOP,m_monitorRect.left,m_monitorRect.top,0,0,SWP_HIDEWINDOW);//HWND_TOP m_settings = static_cast(m_player->get_settings()); m_settings.put_autoStart(FALSE); m_playlist = m_player->get_currentPlaylist(); m_playlist.put_name(_T("可视柜台本地音频播放")); // 每次加载1个文件播放,先播放第一个 for (int i = 0; i < m_pConfig->nFileCnt; i++) { CString audioPath(""); audioPath.Format(_T("%s%s"), (LPCSTR)m_pConfig->strRootPath, (LPCSTR)m_pConfig->strFileNames[i]); m_playlist.appendItem(m_player->newMedia(audioPath)); } m_control = static_cast(m_player->get_controls()); m_control.play(); // // if(m_pConfig->nFileCnt > 0) // { // CString audioPath(""); // audioPath.Format(_T("%s%s"), (LPCSTR)m_pConfig->strRootPath, (LPCSTR)m_pConfig->strFileNames[0]); // m_player->put_URL(audioPath); // // m_control = static_cast(m_player->get_controls()); // m_control.play(); // } } else if (m_pConfig->eMode == LOCALVIDEO) // 本地视频模式播放 { //::SetWindowPos(this->GetSafeHwnd(),HWND_TOP,0,0,0,0,SWP_HIDEWINDOW);//HWND_TO if (m_pConfig->bSimpleMode == true) // 简洁模式 { m_player->put_uiMode(_T("none")); // 去掉标题栏和边框 ModifyStyle(WS_CAPTION, 0, 0); ModifyStyleEx(WS_EX_DLGMODALFRAME, 0, 0); } SetTimer(FULLSCREEN_TIMERID, GetDoubleClickTime(), 0); // 开启Timer监视,防止WMP本身的全屏 m_bTimer = true; m_settings = static_cast(m_player->get_settings()); m_settings.put_autoStart(FALSE); m_settings.put_volume(m_pConfig->nVolume); // 设置音量 add by ly 2017/06/06 m_playlist = m_player->get_currentPlaylist(); m_playlist.put_name(_T("可视柜台本地视频播放")); if (m_pConfig->bFullScreen == true) // 全屏模式 { //最大化 ::SetWindowPos(this->GetSafeHwnd(), HWND_TOPMOST, m_monitorRect.left, m_monitorRect.top,m_monitorRect.Width(),m_monitorRect.Height(),SWP_HIDEWINDOW);//HWND_TOP } else { if (m_pConfig->nWndWidth > m_monitorRect.Width()) { m_pConfig->nWndWidth = m_monitorRect.Width(); } if (m_pConfig->nWndHeight > m_monitorRect.Height()) { m_pConfig->nWndHeight = m_monitorRect.Height(); } ::SetWindowPos(this->GetSafeHwnd(), HWND_TOPMOST, m_monitorRect.left+m_pConfig->nWndX, m_monitorRect.top+m_pConfig->nWndY,m_pConfig->nWndWidth,m_pConfig->nWndHeight,SWP_HIDEWINDOW);//HWND_TOP } // 每次加载1个文件播放,先播放第一个 if(m_pConfig->nFileCnt > 0) { CString videoPath(""); videoPath.Format(_T("%s%s"), (LPCSTR)m_pConfig->strRootPath, (LPCSTR)m_pConfig->strFileNames[0]); m_player->put_URL(videoPath); m_control = static_cast(m_player->get_controls()); m_control.play(); } } } return TRUE; } void CPlayerDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CPlayerDlg, CDialogEx) ON_MESSAGE(WM_SETVOLUME_MESSAGE, &CPlayerDlg::OnSetVolumeMessage) ON_WM_CLOSE() ON_WM_SIZE() ON_WM_NCLBUTTONDBLCLK() ON_WM_NCHITTEST() ON_WM_NCLBUTTONDOWN() ON_WM_TIMER() ON_WM_ERASEBKGND() END_MESSAGE_MAP() // CPlayerDlg 消息处理程序 LRESULT CPlayerDlg::OnSetVolumeMessage( WPARAM wParam, LPARAM lParam ) { int nVolume = (int)lParam; m_settings.put_volume(nVolume); return 0; } void CPlayerDlg::OnClose() { // TODO: 在此添加消息处理程序代码和/或调用默认值 EndPlay(); CDialogEx::OnClose(); } void CPlayerDlg::OnSize(UINT nType, int cx, int cy) { if(!m_bInit) return; CDialogEx::OnSize(nType, cx, cy); // TODO: 在此处添加消息处理程序代码 m_player->MoveWindow(0, 0, cx, cy); } void CPlayerDlg::OnNcLButtonDblClk(UINT nHitTest, CPoint point) { CDialogEx::OnNcLButtonDblClk(nHitTest, point); // TODO: 在此添加消息处理程序代码和/或调用默认值 if(nHitTest == HTCAPTION) { if (!m_bMax) { this->GetWindowRect(&m_savedWndRect);//保存窗口最大化之前的Rect ::SetWindowPos(this->GetSafeHwnd(), HWND_TOPMOST, m_monitorRect.left, m_monitorRect.top,m_monitorRect.Width(),m_monitorRect.Height(),SWP_SHOWWINDOW); } else { ::SetWindowPos(this->GetSafeHwnd(), HWND_TOPMOST, m_savedWndRect.left, m_savedWndRect.top,m_savedWndRect.Width(),m_savedWndRect.Height(),SWP_SHOWWINDOW); } m_bMax = !m_bMax; return; } } void CPlayerDlg::PlayStateChangeOcx1(long NewState) { if(m_bInit) { switch(NewState) { case 3: // 播放状态 { if (m_pConfig->eMode == SINGLE || m_pConfig->eMode == SALESRECORD) { CWMPMedia m_media = m_player->get_currentMedia(); long width = m_media.get_imageSourceWidth(); long height = m_media.get_imageSourceHeight();//HWND_TOPMOST int border_w = GetSystemMetrics(SM_CXFRAME); //窗口边框的边缘宽度 int border_h = GetSystemMetrics(SM_CYFRAME); //窗口边框的边缘高度 int titlebar_h = GetSystemMetrics(SM_CYSIZE); if (width > height) { //long wnd_w = 2.8*width+2*border_w; //long wnd_h = 2.8*height+border_h+titlebar_h+70; //::SetWindowPos(this->GetSafeHwnd(), HWND_TOPMOST, m_monitorRect.left+(m_monitorRect.Width()-wnd_w)/2, // m_monitorRect.top+(m_monitorRect.Height()-wnd_h)/2,wnd_w,wnd_h,SWP_SHOWWINDOW);//HWND_TOP long wnd_w = 1.4*width+2*border_w; long wnd_h = 1.4*height+border_h+titlebar_h+140; ::SetWindowPos(this->GetSafeHwnd(), HWND_TOPMOST, m_monitorRect.left+(m_monitorRect.Width()-wnd_w)/2, m_monitorRect.top+(m_monitorRect.Height()-wnd_h)/2,wnd_w,wnd_h,SWP_SHOWWINDOW);//HWND_TOP } else { //long wnd_w = 1.6*width+2*border_w; //long wnd_h = 1.6*height+border_h+titlebar_h+88; //::SetWindowPos(this->GetSafeHwnd(), HWND_TOPMOST, m_monitorRect.left+(m_monitorRect.Width()-wnd_w)/2+336, // m_monitorRect.top+(m_monitorRect.Height()-wnd_h)/2-54,wnd_w,wnd_h,SWP_SHOWWINDOW);//HWND_TOP long wnd_w = 0.8*width+2*border_w; long wnd_h = 0.8*height+border_h+titlebar_h+128; ::SetWindowPos(this->GetSafeHwnd(), HWND_TOPMOST, m_monitorRect.left+(m_monitorRect.Width()-wnd_w)/2+336, m_monitorRect.top+(m_monitorRect.Height()-wnd_h)/2-54,wnd_w,wnd_h,SWP_SHOWWINDOW);//HWND_TOP } } else if (m_pConfig->eMode == LOCALVIDEO) { this->ShowWindow(SW_SHOW); } } break; case 2: // 暂停状态 break; case 1: // 停止状态 { if (m_pConfig->eMode == LOCALVIDEO) { if (m_pConfig->eMode == LOCALVIDEO) { // 隐藏窗口 this->ShowWindow(SW_HIDE); } m_nMediaInx = (m_nMediaInx+1)%m_pConfig->nFileCnt; if (m_pConfig->nPlayCnt != 0) // 非循环播放 { if (m_nMediaInx == 0) { m_nPlayCnt++; } if (m_pConfig->eMode == LOCALAUDIO) { //EndPlay(); return; } if (m_nPlayCnt == m_pConfig->nPlayCnt)//播放次数到了 { // 结束播放 //EndPlay(); SetEvent(m_pConfig->h_WMPPlayEndEvent); return; } } if (m_pConfig->nPlayInterval >= 0) { SetTimer(INTERVAL_TIMERID, m_pConfig->nPlayInterval, 0); } } } break; case 8: // 播放列表单曲结束 if (m_pConfig->eMode == LOCALAUDIO) { m_pConfig->nFileCnt--; if (m_pConfig->nFileCnt == 0) SetEvent(m_pConfig->h_WMPPlayEndEvent);// play end } break; case 9: // 缓冲状态 case 10:// 开始状态 default: break; } } } BOOL CPlayerDlg::EndPlay() { // 停止计时器 if (m_bTimer) { m_bTimer = false; KillTimer(FULLSCREEN_TIMERID); } // 停止播放 m_bInit = false; // new added KillTimer(INTERVAL_TIMERID); // new added m_control.stop(); m_playlist = m_player->get_currentPlaylist(); // new added m_playlist.clear(); // new added m_player->close(); m_player->DestroyWindow(); // new added // 关闭窗口 EndDialog(IDCANCEL); return TRUE; } LRESULT CPlayerDlg::OnNcHitTest(CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 int ret = CDialogEx::OnNcHitTest(point); if(HTTOP == ret || HTBOTTOM == ret || HTLEFT == ret || HTRIGHT == ret || HTBOTTOMLEFT == ret || HTBOTTOMRIGHT == ret || HTTOPLEFT == ret || HTTOPRIGHT == ret || HTCAPTION == ret) return HTCLIENT; return ret; } void CPlayerDlg::OnNcLButtonDown(UINT nHitTest, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if(HTTOP == nHitTest || HTBOTTOM == nHitTest || HTLEFT == nHitTest || HTRIGHT == nHitTest || HTBOTTOMLEFT == nHitTest || HTBOTTOMRIGHT == nHitTest || HTTOPLEFT == nHitTest || HTTOPRIGHT == nHitTest || HTCAPTION == nHitTest) return; CDialogEx::OnNcLButtonDown(nHitTest, point); return; } void CPlayerDlg::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if (nIDEvent == FULLSCREEN_TIMERID) { if (m_player->get_fullScreen() == TRUE) { m_player->put_fullScreen(FALSE); } } else if (nIDEvent == INTERVAL_TIMERID) { // 每间隔m_nPlayInterval毫秒重复播放1次 CString mediaPath(""); mediaPath.Format(_T("%s%s"), (LPCSTR)m_pConfig->strRootPath, (LPCSTR)m_pConfig->strFileNames[m_nMediaInx]); m_player->put_URL(mediaPath); m_control = static_cast(m_player->get_controls()); m_control.play(); KillTimer(INTERVAL_TIMERID); } CDialogEx::OnTimer(nIDEvent); } // 禁止刷新背景 BOOL CPlayerDlg::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息处理程序代码和/或调用默认值 //return CDialogEx::OnEraseBkgnd(pDC); return TRUE; }