//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
//=========================================================================
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//
//
//=========================================================================
#include <windows.h>
#include <mmsystem.h>
#include <streams.h>
#include <commctrl.h>
#include <commdlg.h>
#include "playwnd.h"
#pragma comment (lib,"Strmiids.lib")
#pragma comment (lib,"ole32.lib")
//
// Global data
//
HWND ghApp=0, ghCB=0;
HINSTANCE ghInst;
TCHAR g_szFileName[MAX_PATH]={0};
PLAYSTATE g_psCurrent=psSTOPPED;
BOOL g_bAudioOnly=FALSE;
LONG g_lVolume=VOLUME_FULL;
// Collection of interfaces
IGraphBuilder *pGB = NULL;
IMediaControl *pMC = NULL;
IMediaEventEx *pME = NULL;
IVideoWindow *pVW = NULL;
IBasicAudio *pBA=NULL;
IBasicVideo *pBV = NULL;
IMediaSeeking *pMS = NULL;
HRESULT PlayMovieInWindow(LPCTSTR szFile)
{
WCHAR wFile[MAX_PATH];
HRESULT hr;
RETAILMSG(1, (TEXT("Playing media %s...\r\n"), szFile));
// Clear open dialog remnants before the slow-running RenderFile()
UpdateWindow(ghApp);
if(FAILED(StringCchCopy(wFile, MAX_PATH, szFile)))
return E_FAIL;
// Get the interface for DirectShow's GraphBuilder
JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGB));
if(SUCCEEDED(hr))
{
// Have the graph construct its the appropriate graph automatically
JIF(pGB->RenderFile(wFile, NULL));
// QueryInterface for DirectShow interfaces
JIF(pGB->QueryInterface(IID_IMediaControl, (void **)&pMC));
JIF(pGB->QueryInterface(IID_IMediaEventEx, (void **)&pME));
JIF(pGB->QueryInterface(IID_IMediaSeeking, (void **)&pMS));
// Query for video interfaces, which may not be relevant for audio files
JIF(pGB->QueryInterface(IID_IVideoWindow, (void **)&pVW));
JIF(pGB->QueryInterface(IID_IBasicVideo, (void **)&pBV));
// Query for audio interfaces, which may not be relevant for video-only files
JIF(pGB->QueryInterface(IID_IBasicAudio, (void **)&pBA));
// Is this an audio-only file (no video component)?
CheckVisibility();
if (!g_bAudioOnly)
{
JIF(pVW->put_Owner((OAHWND)ghApp));
JIF(pVW->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN));
}
// Have the graph signal event via window callbacks for performance
JIF(pME->SetNotifyWindow((OAHWND)ghApp, WM_GRAPHNOTIFY, 0));
if (g_bAudioOnly)
{
JIF(InitAudioWindow());
}
else
{
JIF(InitVideoWindow());
}
// Let's get ready to rumble!
ShowWindow(ghApp, SW_SHOWNORMAL);
UpdateWindow(ghApp);
SetForegroundWindow(ghApp);
SetFocus(ghApp);
UpdateMainTitle();
// Run the graph to play the media file
JIF(pMC->Run());
g_psCurrent=psRUNNING;
SetFocus(ghApp);
}
return hr;
}
HRESULT InitVideoWindow(void)
{
LONG lHeight, lWidth;
HRESULT hr = S_OK;
RECT rect;
JIF(pBV->GetVideoSize(&lWidth, &lHeight));
// RETAILMSG(1, (TEXT(" Video size: %d x %d\r\n"), lWidth, lHeight));
SetWindowPos(ghApp, NULL, 0, 0, lWidth, lHeight, SWP_NOMOVE | SWP_NOOWNERZORDER);
int nTitleHeight = GetSystemMetrics(SM_CYCAPTION);
int nBorderWidth = GetSystemMetrics(SM_CXBORDER);
int nBorderHeight = GetSystemMetrics(SM_CYBORDER);
// Account for size of title bar and borders for exact match
// of window client area to default video size
SetWindowPos(ghApp, NULL, 0, 0, lWidth + 2*nBorderWidth,
lHeight + nTitleHeight + 2*nBorderHeight,
SWP_NOMOVE | SWP_NOOWNERZORDER);
GetClientRect(ghApp, &rect);
JIF(pVW->SetWindowPosition(rect.left, rect.top, rect.right, rect.bottom));
return hr;
}
HRESULT InitAudioWindow(void)
{
SetWindowPos(ghApp, NULL, 0, 0,
DEFAULT_AUDIO_WIDTH,
DEFAULT_AUDIO_HEIGHT,
SWP_NOMOVE | SWP_NOOWNERZORDER);
CommandBar_DrawMenuBar(ghCB, 0);
// Do something different for audio-only files
PaintAudioWindow();
return S_OK;
}
void PaintAudioWindow(void)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
HBRUSH hBrush;
hdc = BeginPaint(ghApp, &ps);
GetClientRect(ghApp, &rect);
// Show white if playing an audio file, gray if not
if (pMC)
hBrush = (HBRUSH) GetStockObject(WHITE_BRUSH);
else
hBrush = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
FillRect(hdc, &rect, hBrush);
EndPaint(ghApp, &ps);
}
void CheckVisibility(void)
{
long lVisible;
HRESULT hr;
g_bAudioOnly = FALSE;
if (!pVW)
{
g_bAudioOnly = TRUE;
RETAILMSG(1, (TEXT("No VideoWindow interface. Assuming audio/MIDI file or unsupported video codec.\r\n")));
return;
}
if (!pBV)
{
g_bAudioOnly = TRUE;
RETAILMSG(1, (TEXT("No BasicVideo interface. Assuming audio/MIDI file or unsupported video codec.\r\n")));
return;
}
hr = pVW->get_Visible(&lVisible);
if (FAILED(hr))
{
// If this is an audio-only clip, get_Visible() won't work.
//
// Also, if this video is encoded with an unsupported codec,
// we won't see any video, although the audio will work if it is
// of a supported format.
//
if (hr == E_NOINTERFACE)
{
g_bAudioOnly = TRUE;
RETAILMSG(1, (TEXT("Video window not visible. Assuming audio/MIDI file or unsupported video codec.\r\n")));
}
else
{
RETAILMSG(1, (TEXT("Failed(%08lx) in pVW->get_Visible()!\r\n"), hr));
}
return;
}
}
void PauseClip(void)
{
if (!pMC)
return;
// Play/pause
if((g_psCurrent == psPAUSED) || (g_psCurrent == psSTOPPED))
{
pMC->Run();
g_psCurrent = psRUNNING;
}
else
{
pMC->Pause();
g_psCurrent = psPAUSED;
}
UpdateMainTitle();
}
void StopClip(void)
{
if ((!pMC) || (!pMS))
return;
// Stop and reset postion to beginning
if((g_psCurrent == psPAUSED) || (g_psCurrent == psRUNNING))
{
LONGLONG pos = 0;
pMC->Stop();
g_psCurrent = psSTOPPED;
pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,
NULL, AM_SEEKING_NoPositioning);
// Display the first frame to indicate the reset condition
pMC->Pause();
}
UpdateMainTitle();
}
void OpenClip()
{
HRESULT hr;
// If no filename specified by command line, show open dlg
if(g_szFileName[0] == L'\0')
{
TCHAR szFilename[MAX_PATH];
UpdateMainTitle();
// If no filename was specified on the command line, then our video
// window has not been created or made visible. Make our mai
te.rar_DirectShow
版权申诉
6 浏览量
2022-09-14
18:39:37
上传
评论
收藏 3.89MB RAR 举报
四散
- 粉丝: 51
- 资源: 1万+
最新资源
- Picasso_v3.1 2.ipa
- chromedriver-mac-arm64.zip
- 蓝zapro.apk
- chromedriver-linux64.zip
- UCAS研一深度学习实验-MNIST手写数字识别python源码+详细注释(高分项目)
- 基于Python和PyTorch框架完成的一个手写数字识别实验源码(带MINIST手写数字数据集)+详细注释(高分项目)
- 基于Matlab在MNIST数据集上利用CNN完成手写体数字识别任务,并实现单层CNN反向传播算法+源代码+文档说明(高分项目)
- NVIDIA驱动、CUDA和Pytorch及其依赖
- 基于SVM多特征融合的微表情识别python源码+项目说明+详细注释(高分课程设计)
- html动态爱心代码一(附源码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈