⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 miniplayer.cpp

📁 这是实现从一个复合码流文件中提取一个节目的程序
💻 CPP
字号:
#include <windows.h>
#include <dshow.h>

#define CLASSNAME "cnMiniPlayer"
 
#define WM_GRAPHNOTIFY WM_APP+1

HWND g_hwnd=NULL;
HINSTANCE g_hinst=NULL;
HANDLE Btn1=NULL;
HANDLE Btn2=NULL;

IGraphBuilder *pGraph=NULL;
IMediaEventEx *pMediaEvent=NULL;
IMediaControl *pMediaControl=NULL;
IMediaSeeking *pMediaSeeking=NULL;

TCHAR szTempFile[MAX_PATH];

//***************************************
//initilize the directshow
BOOL InitPlayer(void){
	CoInitialize(NULL);
	//create the filter graph manager
	HRESULT hr=CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC,IID_IGraphBuilder,(void**)&pGraph);
	if (FAILED(hr)){
		MessageBox(g_hwnd,"Error to create FilterGraph.","MiniPlayer",MB_ICONERROR);
		CoUninitialize();
		return FALSE;
	}
	else{
		//create media event
		hr=pGraph->QueryInterface(IID_IMediaEventEx,(void**)&pMediaEvent);
		if (FAILED(hr)){
			MessageBox(g_hwnd,"Error to create IMediaEvent.","MiniPlayer",MB_ICONERROR);
			pGraph->Release();
			CoUninitialize();
			return FALSE;
		}
		//create media control
		hr=pGraph->QueryInterface(IID_IMediaControl,(void**)&pMediaControl);
		if (FAILED(hr)){
			MessageBox(g_hwnd,"Error to create IMediaControl.","MiniPlayer",MB_ICONERROR);
			pMediaEvent->Release();
			pGraph->Release();
			CoUninitialize();
			return FALSE;
		}
		//create media seek
		hr=pGraph->QueryInterface(IID_IMediaSeeking,(void**)&pMediaSeeking);
		if (FAILED(hr)){
			MessageBox(g_hwnd,"Error to create IMediaSeeking.","MiniPlayer",MB_ICONERROR);
			pMediaControl->Release();
			pMediaEvent->Release();
			pGraph->Release();
			CoUninitialize();
			return FALSE;
		}
		hr=pMediaEvent->SetNotifyWindow((OAHWND)g_hwnd,WM_GRAPHNOTIFY,0);
		if (FAILED(hr)){
			MessageBox(g_hwnd,"Error to set notify window.","MiniPlayer",MB_ICONERROR);
			return FALSE;
		}
	}
	CoUninitialize();
	return TRUE;
}

//********************************
//finalize the direct show
void FreePlayer(void){
	pMediaControl->Stop();
	pMediaSeeking->Release();
	pMediaControl->Release();
	pMediaEvent->Release();
	pGraph->Release();
}

//***************************************************
//save the media resource to file, and to play it
BOOL PlayResource(){
	//get the resource
	HRSRC hrs=FindResource(g_hinst,"Media","Player");
	if (hrs==NULL){
		MessageBox(g_hwnd,"Can not find the resource.","MiniPlayer",MB_ICONERROR);
		return FALSE;
	}
	DWORD ResSize=SizeofResource(g_hinst,hrs);
	HGLOBAL hg=LoadResource(g_hinst,hrs);
	if (hg==NULL){
		MessageBox(g_hwnd,"Can not load the resource.","MiniPlayer",MB_ICONERROR);
		return FALSE;
	}
	LPVOID res=LockResource(hg);
	if (res==NULL){
		MessageBox(g_hwnd,"Can not Lock the resource.","MiniPlayer",MB_ICONERROR);
		return FALSE;
	}
	//get the temp file's name
	if (GetTempPath(MAX_PATH,szTempFile)==0){
		MessageBox(g_hwnd,"Can not get temp path.","MiniPlayer",MB_ICONERROR);
		return FALSE;
	}
	if (GetTempFileName(szTempFile,"~MP",0,szTempFile)==0){
		//the temp file name is like "mpxx.tmp"
		MessageBox(g_hwnd,"Can not get temp file name.","MiniPlayer",MB_ICONERROR);
		return FALSE;
	}
	//save resource to temp file
	HANDLE hf=CreateFile(szTempFile,GENERIC_READ | GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	if (hf==NULL){
		MessageBox(g_hwnd,"Error to create file.","MiniPlayer",MB_ICONERROR);
		return FALSE;
	}
	DWORD bWrite;
	if (WriteFile(hf,res,ResSize,&bWrite,NULL)==0){
		MessageBox(g_hwnd,"Error to write file.","MiniPlayer",MB_ICONERROR);
		return FALSE;
	}
	if (bWrite!=ResSize){
		MessageBox(g_hwnd,"Error to write file 2.","MiniPlayer",MB_ICONERROR);
		return FALSE;
	}
	CloseHandle(hf); 
	//start to play the media
	int cch=lstrlen(szTempFile)+1;
	unsigned short * wFileName=new unsigned short[cch*2];
	if (MultiByteToWideChar(GetACP(),0,szTempFile,-1,wFileName,cch)==0){
		MessageBox(g_hwnd,"Error to convert file name.","MiniPlayer",MB_ICONERROR);
		return FALSE;
	}
	HRESULT hr=(pMediaControl->RenderFile(wFileName));
	if (FAILED(hr)){ //display more informations about why can't render file
		switch(hr){
		case VFW_S_AUDIO_NOT_RENDERED:
			MessageBox(g_hwnd,"Error to render file: Cannot play the audio stream: could not find a suitable renderer.","MiniPlayer",MB_ICONERROR);
			break;
		case VFW_S_DUPLICATE_NAME:
			MessageBox(g_hwnd,"Error to render file: An attempt to add a filter with a duplicate name succeeded with a modified name.","MiniPlayer",MB_ICONERROR);
			break;
		case VFW_S_PARTIAL_RENDER:
			MessageBox(g_hwnd,"Error to render file: Some streams in this movie are in an unsupported format.","MiniPlayer",MB_ICONERROR);
			break;
		case VFW_S_VIDEO_NOT_RENDERED:
			MessageBox(g_hwnd,"Error ro render file: Cannot play the video stream: could not find a suitable renderer.","MiniPlayer",MB_ICONERROR);
			break;
		case E_ABORT:
			MessageBox(g_hwnd,"Error to render file: Operation aborted.","MiniPlayer",MB_ICONERROR);
			break;;
		case VFW_E_CANNOT_CONNECT:
			MessageBox(g_hwnd,"Error to render file: No combination of intermediate filters could be found to make the connection.","MiniPlayer",MB_ICONERROR);
			break;
		case VFW_E_CANNOT_LOAD_SOURCE_FILTER:
			MessageBox(g_hwnd,"Error to render file: The source filter for this file could not be loaded.","MiniPlayer",MB_ICONERROR);
			break;
		case VFW_E_CANNOT_RENDER:
			MessageBox(g_hwnd,"Error to render file: No combination of filters could be found to render the stream.","MiniPlayer",MB_ICONERROR);
			break;
		case VFW_E_INVALID_FILE_FORMAT:
			MessageBox(g_hwnd,"Error to render file: The file format is invalid.","MiniPlayer",MB_ICONERROR);
			break;
		case VFW_E_NOT_FOUND:
			MessageBox(g_hwnd,"Error to render file: An object or name was not found.","MiniPlayer",MB_ICONERROR);
			break;
		case VFW_E_NOT_IN_GRAPH:
			MessageBox(g_hwnd,"Error to render file: Cannot perform the requested function on an object that is not in the filter graph.","MiniPlayer",MB_ICONERROR);
			break;
		case VFW_E_UNKNOWN_FILE_TYPE:
			MessageBox(g_hwnd,"Error to render file: The media type of this file is not recognized.","MiniPlayer",MB_ICONERROR);
			break;
		case VFW_E_UNSUPPORTED_STREAM:
			MessageBox(g_hwnd,"Error to render file: Cannot play the file: the format is not supported.","MiniPlayer",MB_ICONERROR);
			break;
		default:
			MessageBox(g_hwnd,"Error to render file: Unknown error.","MiniPlayer",MB_ICONERROR);
			break;
		}
		return FALSE;
	}
	hr=pMediaControl->Run();
	if (FAILED(hr)){
		MessageBox(g_hwnd,"Error to run media control.","MiniPlayer",MB_ICONERROR);
		return FALSE;
	}
	return TRUE;
}

//to get the notify of directshow
void HandleEvent(){
	long evCode,param1,param2;
	HRESULT hr;
	while(hr=pMediaEvent->GetEvent(&evCode,&param1,&param2,0),SUCCEEDED(hr)){
		hr=pMediaEvent->FreeEventParams(evCode,param1,param2);
		if((EC_COMPLETE==evCode) || (EC_USERABORT==evCode)){
			hr=pMediaControl->Stop();
			if (FAILED(hr)){
				MessageBox(g_hwnd,"Error to stop the media.","MiniPlayer",MB_ICONERROR);
				return;
			}
			LONGLONG pos=0;
			hr=pMediaSeeking->SetPositions(&pos,AM_SEEKING_AbsolutePositioning,NULL,AM_SEEKING_NoPositioning);
			if (FAILED(hr)){
				MessageBox(g_hwnd,"Error to set position.","MiniPlayer",MB_ICONERROR);
				return;
			}
			SetWindowText((HWND)Btn1,">");
			break;
		}
	}
}

//Message Handle
long WINAPI WindowProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
	HRESULT hr;
	switch(msg){
	case WM_GRAPHNOTIFY:
		HandleEvent();
		break;
	case WM_COMMAND:
		if (lParam==(long)Btn1){
			FILTER_STATE fs;
			hr=pMediaControl->GetState(0,(long *)&fs);
			if (FAILED(hr)){
				MessageBox(g_hwnd,"Error to get state.","MiniPlayer",MB_ICONERROR);
				return 0;
			}
			if (fs==State_Running){ //is running, to pause it
				hr=pMediaControl->Pause();
				if (FAILED(hr)){
					MessageBox(g_hwnd,"Error to pause the media.","MiniPlayer",MB_ICONERROR);
					return 0;
				}
				SetWindowText((HWND)Btn1,">");
			}
			else{ //is puased, run it
				hr=pMediaControl->Run();
				if (FAILED(hr)){
					MessageBox(g_hwnd,"Error to run the media.","MiniPlayer",MB_ICONERROR);
					return 0;
				}
				SetWindowText((HWND)Btn1,"||");
			}
		}
		else if (lParam==(long)Btn2){
			hr=pMediaControl->Stop();
			if (FAILED(hr)){
				MessageBox(g_hwnd,"Error to stop the media.","MiniPlayer",MB_ICONERROR);
				return 0;
			}
			LONGLONG pos=0;
			hr=pMediaSeeking->SetPositions(&pos,AM_SEEKING_AbsolutePositioning,NULL,AM_SEEKING_NoPositioning);
			if (FAILED(hr)){
				MessageBox(g_hwnd,"Error to set position.","MiniPlayer",MB_ICONERROR);
				return 0;
			}
			SetWindowText((HWND)Btn1,">");
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return (DefWindowProc(hwnd,msg,wParam,lParam));
	}
	return (NULL);
}

//Main
int PASCAL WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nCmdShow){
	WNDCLASS WndClass;
	WndClass.style=0;
	WndClass.lpfnWndProc=WindowProc;
	WndClass.cbClsExtra=0;
	WndClass.cbWndExtra=0;
	WndClass.hInstance=hInst;
	WndClass.hIcon=LoadIcon(hInst,"MAIN");
	WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	WndClass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
	WndClass.lpszMenuName=NULL;
	WndClass.lpszClassName=CLASSNAME;
	if(!RegisterClass(&WndClass))
		return 0;
	g_hwnd=CreateWindow(CLASSNAME,"MiniPlayer",WS_SYSMENU,GetSystemMetrics(SM_CXFULLSCREEN)-130,GetSystemMetrics(SM_CYFULLSCREEN)+GetSystemMetrics(SM_CYCAPTION)-56,130,56,NULL,NULL,hInst,NULL);
	if(!g_hwnd)
		return 0;
	Btn1=CreateWindow("Button",">",WS_VISIBLE | WS_CHILD | BS_PUSHLIKE | BS_TEXT,10,7,20,20,g_hwnd,0,hInst,NULL);
	Btn2=CreateWindow("Button","X",WS_VISIBLE | WS_CHILD | BS_PUSHLIKE | BS_TEXT,40,7,20,20,g_hwnd,0,hInst,NULL);
	ShowWindow(g_hwnd,nCmdShow);
	UpdateWindow(g_hwnd);
	g_hinst=hInst;
	if (InitPlayer()){
		//play media automatically
		if (PlayResource())
			SetWindowText((HWND)Btn1,"||");
		MSG msg;
		while(GetMessage(&msg,NULL,0,0)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		DeleteFile(szTempFile); // delete the temp file
		FreePlayer();
		return msg.wParam;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -