解析音乐播放器实现
简单通俗易懂的方式实现音乐播放功能,阅读代码仅需要C/C++简单基础知识,实现该音乐播放器只使用了microsoft directx sdk几个多媒体控制函数。
#include <dshow.h>
void main(void)
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return;
}
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
printf("ERROR - Could not create the Filter Graph Manager.");
return;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->RenderFile(L"C:\\Example.avi", NULL);
if (SUCCEEDED(hr))
{
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
}
}
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
}
基于该例子添加各种控制实现音乐播放器功能
mpx代码解析
mpx工程代码解析
包含mpx.h头文件和mpx.c实现文件
该工程只生成控制音乐播放器用到的静态库mpx.lib
mpx.h头文件源码
/*****************************************************************************
* author menghun3@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* source code address https://github.com/menghun3/mpx
******************************************************************************/
#ifndef MPX_H
#define MPX_H
void mpxInit();
void mpxDestroy();
void mpxPlayFile(void *path);
void mpxPlay();
void mpxPause();
void mpxStop();
int mpxGetCurrentPosition(long long *curpos);
int mpxGetPositions(long long *curpos, long long *stoppos);
int mpxGetStopPosition(long long *stoppos);
int mpxGetVolume(long *plVolume);
int mpxPutVolume(long lVolume);
int mpxGetGuidFormat(GUID *pFormat);
int mpxGetRate(double *dRate);
#endif // MPX_H
使用者只需要调用mpxPlayFile()函数将歌曲文件路径传入即可播放文件,然后使用mpxPause()、mpxStop()、mpxPlay()控制歌曲的暂停、停止、播放。
mpx.c实现文件代码分段解析
完整代码见https://github.com/menghun3/mpx/blob/master/mpx/mpx.c
全局变量
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
IMediaSeeking *pSeek = NULL;
IBasicAudio *pBA = NULL;
HANDLE h = NULL;
int threadid = 0;
HANDLE checkh = NULL;
int chechid = 0;
mpxPlayFile函数
// 播放文件
void mpxPlayFile(void *path)
{
if (h)
{
PostThreadMessage(threadid, PT_QUIT, (WPARAM)0, (LPARAM)0);
WaitForSingleObject(h, INFINITE);
}
h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlayThread, path, 0, (LPDWORD)&threadid);
if (NULL == h)
{
printf("create thread error\n");
return;
}
}
该函数创建一个线程控制播放,主要功能在线程PlayThread()函数里面,threadid为全局变量用于结束该线程。
PlayThread函数
void PlayThread(void *param)
{
WCHAR *songpath = (WCHAR *)param;
PT_CTROL ptstatus = PT_LAST;
InitPlay();
mpPlay1(songpath);
ptstatus = PT_PLAY;
do
{
MSG msg;
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
switch (msg.message)
{
case PT_LOADING:
{
WCHAR *path = (WCHAR*)msg.lParam;
ptstatus = PT_PLAY;
mpPlay1(path);
}
break;
case PT_PLAY:
{
ptstatus = PT_PLAY;
mpPlay();
}
break;
case PT_PAUSE:
{
ptstatus = PT_PAUSE;
mpPause();
}
break;
case PT_STOP:
{
ptstatus = PT_STOP;
mpStop();
}
break;
case PT_QUIT:
{
ptstatus = PT_QUIT;
mpStop();
if (checkh)
{
CloseHandle(checkh);
checkh = NULL;
}
DestroyPlay();
return;
}
break;
default:
{
}
break;
}
}
} while (1);
}
该线程函数实现流程为,初始化播放文件需要的环境InitPlay(),然后执行播放文件mpPlay1 (),再进入消息循环体等待控制信息。
PT_CTROL ptstatus = PT_LAST;用于控制播放状态
typedef enum
{
PT_LOADING = 1,
PT_PLAY,
PT_PAUSE,
PT_STOP,
PT_QUIT,
PT_LAST
}PT_CTROL;
InitPlay函数
void InitPlay()
{
HRESULT hr = 0;
hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("init com error\n");
return;
}
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
printf("create fgm error\n");
return ;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
if (FAILED(hr))
{
printf("get control error\n");
return ;
}
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
if (FAILED(hr))
{
printf("get event error\n");
return;
}
hr = pGraph->QueryInterface(IID_IMediaSeeking, (void **)&pSeek);
if (FAILED(hr))
{
printf("get seek error\n");
return;
}
}
该函数封装了MSDN例子中初始化环境部分,初始化com,初始化IGraphBuilder对象,及通过IGraphBuilder对象获取以下对象
IMediaControl*pControl=NULL;
IMediaEvent *pEvent = NULL;
IMediaSeeking*pSeek=NULL;
IBasicAudio *pBA = NULL;
mpPlay1函数
void mpPlay1(WCHAR *playSongPath)
{
HRESULT hr;
hr = pGraph->RenderFile(playSongPath,NULL);
if (SUCCEEDED(hr))
{
hr = pControl->Run();
if (FAILED(hr))
{
printf("run error\n");
}
checkh = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)CheckThread, NULL, 0, (LPDWORD)&chechid);
if (checkh == NULL)
{
printf("create chech thread error\n");
}
}
else
{
printf("render file error\n");
}
}
该函数封装MSDN例子中渲染文件及控制运行文件部分代码,此时音乐已经播放,即使没有其他代码音乐一样可以播放完整。
此函数中又创建一个线程,该线程用于检查播放是否到达末尾,每秒检查一次,如果到达末尾则向控制播放的线程发送结束命令,此时控制播放的线程会清理环境。
CheckThread函数
void CheckThread(void *param)
{
HRESULT hr;
while (1)
{
long long stoppos = 0;
long long curpos = 0;
Sleep(1000);
hr = pSeek->GetPositions(&curpos, &stoppos);
if (curpos == stoppos)
{
PostThreadMessage(threadid, PT_QUIT, NULL, NULL);
printf("play end\n");
return;
}
}
}
通过IMediaSeeking对象获取当前播放位置及结束位置并作比较,如果相等则说明播放完毕,此时向控制线程发送PT_QUIT命令表示播放结束。
mpPlay、mpStop、mpPause控制函数
这几个函数用户控制音乐播放,实现最简单,被PlayThread线程函数调用控制播放。
void mpPlay()
{
IMediaControl_Run(pControl);
}
void mpStop()
{
HRESULT hr = 0;
long long startpos = 0;
hr =IMediaSeeking_SetPositions(pSeek, &startpos, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);
hr = IMediaControl_Stop(pControl);
}
void mpPause()
{
HRESULT hr = 0;
hr = IMediaControl_Pause(pControl);
}
PlayThread调用这几个函数代码如下
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
switch (msg.message)
{
case PT_LOADING:
{
WCHAR *path = (WCHAR*)msg.lParam;
ptstatus = PT_PLAY;
mpPlay1(path);
}
break;
case PT_PLAY:
{
ptstatus = PT_PLAY;
mpPlay();
}
break;
case PT_PAUSE:
{
ptstatus = PT_PAUSE;
mpPause();
}
break;
case PT_STOP:
{
ptstatus = PT_STOP;
mpStop();
}
break;
case PT_QUIT:
{
ptstatus = PT_QUIT;
mpStop();
if (checkh)
{
CloseHandle(checkh);
checkh = NULL;
}
DestroyPlay();
return;
}
break;
default:
{
}
break;
}
}
DestroyPlay函数
void DestroyPlay()
{
IBasicAudio_Release(pBA);
pBA = NULL;
IMediaSeeking_Release(pSeek);
pSeek = NULL;
IMediaControl_Release(pControl);
pControl = NULL;
IMediaEvent_Release(pEvent);
pEvent = NULL;
IGraphBuilder_Release(pGraph);
pGraph = NULL;
CoUninitialize();
}
释放对象
mpxPlay、mpxPause、mpxStop对外接口
// 播放
void mpxPlay()
{
PostThreadMessage(threadid, PT_PLAY, (WPARAM)0, (LPARAM)0);
}
// 暂停
void mpxPause()
{
PostThreadMessage(threadid, PT_PAUSE, (WPARAM)0, (LPARAM)0);
}
// 停止
void mpxStop()
{
PostThreadMessage(threadid, PT_STOP, (WPARAM)0, (LPARAM)0);
}
向控制线程发送播放、暂停、停止控制命令。
至此主要功能已经实现,测试程序只要调用
voidmpxPlayFile(void*path);
voidmpxPlay();
voidmpxPause();
voidmpxStop();
这四个函数即可完整控制音乐的播放、暂停、和停止。