📄 simpleplayerdlg.cpp
字号:
// SimplePlayerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SimplePlayer.h"
#include "SimplePlayerDlg.h"
#include "CDXGraph.h"
#include "CMediaSocketClient.h"
#include "CDataAdmin.h"
#include "CListenSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSimplePlayerDlg dialog
CSimplePlayerDlg::CSimplePlayerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSimplePlayerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSimplePlayerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
mFilterGraph = NULL;
m_pDataList = NULL;
mSourceFile = NULL;
mSliderTimer = 0;
}
void CSimplePlayerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSimplePlayerDlg)
DDX_Control(pDX, IDC_SLIDER_GRAPH, mSliderGraph);
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSimplePlayerDlg, CDialog)
//{{AFX_MSG_MAP(CSimplePlayerDlg)
ON_WM_HELPINFO()
ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
ON_BN_CLICKED(IDC_BUTTON_PAUSE, OnButtonPause)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
ON_WM_TIMER()
ON_WM_HSCROLL()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_GRAPHNOTIFY, OnGraphNotify)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSimplePlayerDlg message handlers
BOOL CSimplePlayerDlg::OnInitDialog()
{
RETAILMSG( 1, ( TEXT(" INIT\r\n ") ) );
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
// COM initialize...
if (FAILED(CoInitialize(NULL)))
{
// AfxMessageBox(" COM初始化失败 ");
return FALSE;
}
//创建一个数据接收队列管理器
m_pDataList = new CDataAdmin();
//创建一个Fliter Graph封装类对象
mFilterGraph = new CDXGraph(m_pDataList);
//创建Fliter Graph
mFilterGraph->Create();
//初始化进度控件
mSliderGraph.SetRange(0, 1000);
mFilterGraph->Create();
mSliderGraph.SetPos(0);
//创建一个监听的Socket
bool pass = true;
m_pListener = new CListenSocket();
//监听的端口为BASE_SOCKET_PORT,被定义为10025
pass = m_pListener->Create(BASE_SOCKET_PORT);
if (pass)
{
//开始监听是否有网络计算机连接
pass = m_pListener->StartListen();
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CSimplePlayerDlg::OnHelpInfo()
{
// TODO: implement help here
MessageBox(_T("Help"));
}
void CSimplePlayerDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
// Release buffers
SAFE_DELETE(mFilterGraph)
Sleep(1000);
SAFE_DELETE(m_pDataList)
// Finished with COM
CoUninitialize();
}
void CSimplePlayerDlg::OnButtonOpen()
{
RETAILMSG( 1, ( TEXT(" OPEN \r\n ") ) );
// TODO: Add your control notification handler code here
PWSTR strFilter = L"mp3 rm wma File (*.mp3;*.rm;*.wma)|*.mp3;*.rm;*.wma|"
L"All Files (*.*)|*.*||";
CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
strFilter);
if (IDOK == dlgOpen.DoModal())
{
CString PathName = dlgOpen.GetPathName();
PathName.Format(_T("%s"), dlgOpen.GetPathName());
mSourceFile = PathName.GetBuffer(MAX_PATH);
// Rebuild the file playback filter graph
CreateGraph();
}
}
void CSimplePlayerDlg::OnButtonPlay()
{
RETAILMSG( 1, ( TEXT(" PLAY \r\n ") ) );
// TODO: Add your control notification handler code here
if (mFilterGraph)
{
mFilterGraph->Run();
// Start a timer
if (mSliderTimer == 0)
{
mSliderTimer = SetTimer(SLIDER_TIMER, 100, NULL);
}
}
}
void CSimplePlayerDlg::OnButtonPause()
{
// TODO: Add your control notification handler code here
if (mFilterGraph)
{
mFilterGraph->Pause();
// Start a timer
if (mSliderTimer == 0)
{
mSliderTimer = SetTimer(SLIDER_TIMER, 100, NULL);
}
}
}
void CSimplePlayerDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
if (mFilterGraph)
{
mFilterGraph->SetCurrentPosition(0);
mFilterGraph->Stop();
// Stop the timer
if (mSliderTimer)
{
KillTimer(mSliderTimer);
mSliderTimer = 0;
}
}
}
//创建Fliter Graph
void CSimplePlayerDlg::CreateGraph()
{
DestroyGraph();
if (mFilterGraph->Create())
{
// Render the source clip
int iSize;
char* pszMultiByte;
//获得所需缓冲区大小
iSize = WideCharToMultiByte(CP_ACP, 0, mSourceFile, -1, NULL, 0, NULL, NULL);
//为缓冲区分配空间
pszMultiByte = (char*)malloc((iSize+1)/**sizeof(char)*/);
//填充缓冲区
WideCharToMultiByte(CP_ACP, 0, mSourceFile, -1, pszMultiByte, iSize, NULL, NULL);
// RETAILMSG( 1, ( TEXT(" pathname \r\n "), pszMultiByte ) );
mFilterGraph->RenderFile(pszMultiByte);
// Show the first frame
mFilterGraph->Pause();
}
}
void CSimplePlayerDlg::DestroyGraph(void)
{
if (mFilterGraph)
{
// Stop the filter graph first
mFilterGraph->Stop();
delete mFilterGraph;
mFilterGraph = NULL;
}
}
//定时器消息处理函数 ,实现进度条的即时显示
void CSimplePlayerDlg::OnTimer(UINT nIDEvent)
{
if (nIDEvent == mSliderTimer && mFilterGraph)
{
double pos = 0, duration = 1.;
mFilterGraph->GetCurrentPosition(&pos);//获得当前的播放时间点
mFilterGraph->GetDuration(&duration);
// Get the new position, and update the slider
int newPos = int(pos * 1000 / duration);
if (mSliderGraph.GetPos() != newPos)
{
mSliderGraph.SetPos(newPos); //设置进度控件的当前位置值
}
}
CDialog::OnTimer(nIDEvent);
}
BOOL CSimplePlayerDlg::DestroyWindow()
{
if (mSliderTimer)
{
KillTimer(mSliderTimer);
mSliderTimer = 0;
}
return CDialog::DestroyWindow();
}
//实现进度条的随机拖动
void CSimplePlayerDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if (pScrollBar->GetSafeHwnd() == mSliderGraph.GetSafeHwnd())
{
if (mFilterGraph)
{
// Calculate the current seeking position
double duration = 1.;
mFilterGraph->GetDuration(&duration);
double pos = duration * mSliderGraph.GetPos() / 1000.;
mFilterGraph->SetCurrentPosition(pos);
}
}
else
{
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
}
// Deal with the graph events ,处理Fliter Graph事件,消息响应函数
LRESULT CSimplePlayerDlg::OnGraphNotify(WPARAM inWParam, LPARAM inLParam)
{
IMediaEventEx * pEvent = NULL;
//在消息响应函数中获取Fliter Graph的事件通知,并作相应处理
if (mFilterGraph && (pEvent = mFilterGraph->GetEventHandle()))
{
LONG eventCode = 0, eventParam1 = 0, eventParam2 = 0;
while (SUCCEEDED(pEvent->GetEvent(&eventCode, &eventParam1, &eventParam2, 0)))
{
// Spin through the events
pEvent->FreeEventParams(eventCode, eventParam1, eventParam2);
switch (eventCode)
{
case EC_COMPLETE://Fliter Graph中的数据回放完毕
OnButtonPause();
mFilterGraph->SetCurrentPosition(0);
break;
case EC_USERABORT://用户中断回放
case EC_ERRORABORT://Fliter Graph运行出错
OnButtonStop();
break;
default:
break;
}
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -