http://www.cnblogs.com/MikeZhang/tag/%E8%A7%86%E9%A2%91%E6%8A%80%E6%9C%AF/
使用VLC的SDK开发播放器,不管是播放文件还是RTSP流都是相当简单的事,只需要调用几个简单的函数就可以实现播放了,甚至你连窗口都可以不建,SDK会自动创建窗口。
#include "stdafx.h"
#include
#include
#include "include/rmrtspcltsdk.h"
#include "pubdef.h"
using
namespace
std;
#include
#include
#pragma comment(lib, "libvlc.lib")
#pragma comment(lib, "libvlccore.lib")
libvlc_instance_t * g_vlc_ins = 0;
namespace
RM{
namespace
RTSPCLTSDK
{
class
RtspClientState
{
public
:
RtspClientState()
{
pEndCB = 0;
vlc_ins = g_vlc_ins;
vlc_player = 0;
vlc_media = 0;
m_bOpen =
false
;
m_bPlaying =
false
;
m_hWnd = 0;
}
~RtspClientState(){
;
}
bool
ReInit(CRtspClient* p);
bool
m_bOpen;
bool
m_bPlaying;
string m_strURL;
bool
m_bAutoPlay;
bool
m_bEnableSound;
libvlc_instance_t * vlc_ins ;
//= NULL;
libvlc_media_player_t * vlc_player ;
//= NULL;
libvlc_media_t * vlc_media ;
//= NULL;
CRtspClient::PLAYEND_CB pEndCB;
void
* pEndData;
HWND
m_hWnd;
};
CRtspClient::CRtspClient()
{
_pState =
new
RtspClientState();
}
CRtspClient::~CRtspClient(){
Stop();
delete
_pState;
}
CRtspClient* CRtspClient::createInstance()
{
return
new
CRtspClient();
}
void
CRtspClient::freeInstance(CRtspClient* p)
{
if
(p){
delete
p;
}
}
bool
CRtspClient::Play()
{
if
(_pState->m_bPlaying)
return
true
;
if
( !_pState->vlc_player || !_pState->vlc_media )
{
if
(!_pState->ReInit(
this
))
return
false
;
}
{
libvlc_media_player_set_hwnd(_pState->vlc_player,(
void
*) _pState->m_hWnd);
libvlc_media_player_play(_pState->vlc_player);
_pState->m_bPlaying =
true
;
return
true
;
}
return
false
;
}
bool
CRtspClient::Stop()
{
_pState->m_bPlaying =
false
;
if
( _pState->vlc_player )
libvlc_media_player_stop( _pState->vlc_player);
if
(_pState->vlc_media)
libvlc_media_release(_pState->vlc_media);
if
(_pState->vlc_player)
libvlc_media_player_release(_pState->vlc_player);
_pState->vlc_media = 0;
_pState->vlc_player = 0;
_pState->m_bOpen =
false
;
return
true
;
}
bool
CRtspClient::Pause(
bool
bResume)
{
if
( _pState->vlc_player )
{
if
(bResume)
{
libvlc_media_player_pause( _pState->vlc_player);
_pState->m_bPlaying =
false
;
}
else
{
libvlc_media_player_play( _pState->vlc_player);
_pState->m_bPlaying =
true
;
}
return
true
;
}
return
false
;
}
bool
CRtspClient::Seek(unsigned
long
ul)
{
if
( _pState->vlc_player )
{
::libvlc_media_player_set_time( _pState->vlc_player, ul );
return
true
;
}
return
false
;
}
unsigned
long
CRtspClient::GetPlayedTime()
const
{
if
(_pState->vlc_player)
{
libvlc_time_t t = ::libvlc_media_player_get_time(_pState->vlc_player);
if
( t == -1 )
{
printf
(
"No media now -- GetPlayedTimed\n"
);
return
-1;
}
else
{
return
t;
}
}
return
0;
}
unsigned
long
CRtspClient::GetTotalTime()
const
{
if
(_pState->vlc_media)
return
(unsigned
long
)libvlc_media_get_duration(_pState->vlc_media);
return
0;
}
bool
RtspClientState::ReInit(CRtspClient* p )
{
if
(!vlc_player)
vlc_player = libvlc_media_player_new(vlc_ins);
if
(!vlc_media)
vlc_media = libvlc_media_new_path(vlc_ins,
this
->m_strURL.c_str());
if
( !vlc_media || !vlc_player ) {
::OutputDebugStringA(
"init player or media failed!\n"
);
return
false
;
}
//libvlc_media_parse(vlc_media);
libvlc_media_parse_async( vlc_media);
libvlc_media_player_set_media(vlc_player, vlc_media);
return
true
;
}
bool
CRtspClient::Init( unsigned
long
hWnd,
const
char
* RtspUrl ,
bool
enableSound,
bool
autoPlay)
{
if
(!RtspUrl)
return
false
;
if
(_pState->m_bOpen) Stop();
_pState->m_hWnd = (
HWND
)hWnd;
_pState->m_strURL = RtspUrl;
_pState->m_bAutoPlay = autoPlay;
_pState->m_bEnableSound = enableSound;
if
(!_pState->ReInit(
this
))
return
false
;
_pState->m_bOpen =
true
;
if
(autoPlay)
return
Play();
return
true
;
}
void
CRtspClient::SetNewHwnd( unsigned
long
hWnd)
{
if
(_pState->vlc_player)
libvlc_media_player_set_hwnd(_pState->vlc_player,(
void
*) hWnd);
}
void
CRtspClient::Refersh()
{
}
void
CRtspClient::SetEndCB( PLAYEND_CB cb ,
void
* pData)
{
_pState->pEndCB = cb;
_pState->pEndData = pData;
}
}
}
const
char
* vlc_args[] =
{
"-I"
,
"dummy"
,
"--ignore-config"
,
"--extraintf=logger"
,
"--verbose=2"
,
};
void
DllEntry(
HINSTANCE
dll)
{
g_vlc_ins = libvlc_new(
sizeof
(vlc_args)/
sizeof
(vlc_args[0]), vlc_args );
}
void
DllExit()
{
::libvlc_release(g_vlc_ins);
}
你需要准备的是安装VLC,可以到官网上下载最新版本,直接安装之后在程序目录有一个APP/SDK/里面的include,lib复制出来,另外 有一个插件目录plugins,也要在发布程序时复制到程序根目录就可以了
通过这个代码片断你应该了解如何调用VLC的SDK来实现简单的播放器开发了。