📄 mplayer.cpp
字号:
return false;
}
return true;
}
// audio
bool TMPlayer::SetVolume(float Volume)
{
if(!IsVlcLoaded())
return false;
if(VlcIntf->set_volume(hVlc, Volume) < 0)
return false;
return true;
}
float TMPlayer::GetVolume()
{
float Volume;
if(!IsVlcLoaded())
return 0;
if((Volume = VlcIntf->get_volume(hVlc)) < 0)
return 0;
return Volume;
}
bool TMPlayer::Mute(bool Mute)
{
if(!IsVlcLoaded())
return false;
if(VlcIntf->set_mute(hVlc,Mute) < 0)
return false;
return true;
}
bool TMPlayer::IsMuted()
{
int Mute;
if(!IsVlcLoaded())
return false;
if((Mute = VlcIntf->get_mute(hVlc)) < 0)
return false; // hmm
return Mute;
}
bool TMPlayer::ToggleMute()
{
if(!IsVlcLoaded())
return false;
if(VlcIntf->set_mute(hVlc,-1) < 0)
return false;
return true;
}
// protected
bool TMPlayer::LoadVlc(int LogLevel)
{
if(IsVlcLoaded())
return true;
// load dll
if((hVlcDll = LoadLibrary(SIMPLEVLC_DLL)) == NULL)
return false;
// get entry points
SVLC_GetInterface_t pSVLC_GetInterface;
SVLC_GetVersion_t pSVLC_GetVersion;
SVLC_Initialize_t pSVLC_Initialize;
SVLC_Shutdown_t pSVLC_Shutdown;
pSVLC_GetInterface = (SVLC_GetInterface_t)GetProcAddress(hVlcDll, "SVLC_GetInterface");
pSVLC_GetVersion = (SVLC_GetVersion_t)GetProcAddress(hVlcDll, "SVLC_GetVersion");
pSVLC_Initialize = (SVLC_Initialize_t)GetProcAddress(hVlcDll, "SVLC_Initialize");
pSVLC_Shutdown = (SVLC_Shutdown_t)GetProcAddress(hVlcDll, "SVLC_Shutdown");
if(!pSVLC_GetInterface || !pSVLC_GetVersion || !pSVLC_Initialize || !pSVLC_Shutdown) {
FreeLibrary(hVlcDll);
hVlcDll = NULL;
return false;
}
// init dll
if(pSVLC_Initialize() < 0) {
FreeLibrary(hVlcDll);
hVlcDll = NULL;
return false;
}
// check version
int Major, Minor, Micro;
pSVLC_GetVersion(&Major, &Minor, &Micro);
if(Major != REQ_SVLC_VERSION_MAJOR || Minor != REQ_SVLC_VERSION_MINOR) {
pSVLC_Shutdown();
FreeLibrary(hVlcDll);
hVlcDll = NULL;
return false;
}
// get interface
VlcIntf = new SVlcInterface;
if(pSVLC_GetInterface(VlcIntf) < 0) {
pSVLC_Shutdown();
FreeLibrary(hVlcDll);
hVlcDll = NULL;
VlcIntf = NULL;
return false;
}
// create svlc instance
if((hVlc = VlcIntf->create(LogLevel)) == NULL) {
pSVLC_Shutdown();
FreeLibrary(hVlcDll);
hVlcDll = NULL;
VlcIntf = NULL;
return false;
}
// set udata to us
VlcIntf->set_udata(hVlc, this);
// register callbacks
VlcIntf->set_callback (hVlc, gSVlcCallbackFunc,
SVLC_CB_DISPLAY_POPUP |
SVLC_CB_STATE_CHANGE |
SVLC_CB_POSITION_CHANGE |
SVLC_CB_KEY_PRESSED);
return true;
}
bool TMPlayer::UnloadVlc()
{
if(!IsVlcLoaded())
return true;
// destroy svlc instance
VlcIntf->destroy(hVlc);
hVlc = NULL;
// free interface
delete VlcIntf;
VlcIntf = NULL;
// shutdown library
SVLC_Shutdown_t pSVLC_Shutdown;
if((pSVLC_Shutdown = (SVLC_Shutdown_t)GetProcAddress(hVlcDll, "SVLC_Shutdown")) == NULL)
pSVLC_Shutdown();
// free dll
FreeLibrary(hVlcDll);
hVlcDll = NULL;
return true;
}
bool TMPlayer::IsVlcLoaded()
{
return (hVlcDll && hVlc && VlcIntf);
}
void __fastcall TMPlayer::WMSVlcCallback(TMessage& Msg)
{
SVlcCallbackEvent event = (SVlcCallbackEvent)Msg.WParam;
switch(event) {
case SVLC_CB_STATE_CHANGE: {
// call user
if(FOnStateChange)
FOnStateChange(this, ConvertVlcState(((SVlcCbStateData*)Msg.LParam)->new_state));
break;
}
case SVLC_CB_POSITION_CHANGE: {
// call user
if(FOnPositionChange)
FOnPositionChange(this,
((SVlcCbPositionChangeData*)Msg.LParam)->position,
((SVlcCbPositionChangeData*)Msg.LParam)->duration);
break;
}
case SVLC_CB_DISPLAY_POPUP: {
// call user
if(FOnDisplayPopup)
FOnDisplayPopup(this, ((SVlcCbDisplayPopupData*)Msg.LParam)->show);
break;
}
case SVLC_CB_KEY_PRESSED: {
if(!FOnKeyPress)
break;
int VlcKey = ((SVlcCbKeyPressedData*)Msg.LParam)->key;
char Key = 0;
// do some converting
if(VlcKey & SVLC_KEY_ASCII) {
Key = (char)(VlcKey & SVLC_KEY_ASCII);
} else {
switch(VlcKey) {
case SVLC_KEY_SPACE: Key = ' ';
case SVLC_KEY_ENTER: Key = '\n';
case SVLC_KEY_ESC: Key = 27;
case SVLC_KEY_TAB: Key = '\t';
case SVLC_KEY_BACKSPACE: Key = '\r';
}
}
// call user
if(Key > 0)
FOnKeyPress(this, Key);
break;
}
};
}
bool TMPlayer::CreateDisplayWnd()
{
if(DisplayWnd)
return true;
// create display window calss
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_NOCLOSE;// | CS_DBLCLKS;
wc.lpfnWndProc = (WNDPROC)gDisplayWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = ::GetModuleHandle(NULL);
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "MPlayer VLC Window";
wc.hIconSm = NULL;
if(!::RegisterClassEx(&wc)) {
WNDCLASS wndclass;
// check why it failed, if the class already exists that's fine
if(!::GetClassInfo(::GetModuleHandle(NULL),"MPlayer VLC Window",&wndclass))
return false;
}
// create the window
DisplayWnd = ::CreateWindow("MPlayer VLC Window", "MPlayer VLC Window",
WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN,
0, 0, Width, Height,
Handle, // parent
NULL,
::GetModuleHandle(NULL),
NULL);
if(!DisplayWnd)
return false;
// set this pointer as window property. used in window proc.
::SetProp(DisplayWnd, "MPlayer", (HANDLE)this);
return true;
}
bool TMPlayer::DestroyDisplayWnd()
{
if(!DisplayWnd)
return true;
// remove window property
::RemoveProp(DisplayWnd, "MPlayer");
::DestroyWindow(DisplayWnd);
DisplayWnd = NULL;
return true;
}
long FAR PASCAL TMPlayer::DisplayWndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
if(hWnd != DisplayWnd)
return ::DefWindowProc(hWnd,Msg,wParam,lParam); // shouldn't happen
// this happens of there is no vlc window, e.g. for audio only files
if(Msg == WM_RBUTTONUP && FOnDisplayPopup)
FOnDisplayPopup(this, true);
return ::DefWindowProc(hWnd,Msg,wParam,lParam);
}
TMPlayerState TMPlayer::ConvertVlcState(SVlcPlaybackState VlcState)
{
switch(VlcState)
{
case SVLC_PLAYBACK_CLOSED: return MPClosed;
case SVLC_PLAYBACK_LOADING: return MPLoading;
case SVLC_PLAYBACK_OPEN: return MPOpen;
case SVLC_PLAYBACK_PLAYING: return MPPlaying;
case SVLC_PLAYBACK_PAUSED: return MPPaused;
case SVLC_PLAYBACK_STOPPED: return MPStopped;
case SVLC_PLAYBACK_ERROR: return MPError;
}
return MPError;
}
void __fastcall TMPlayer::Resize(void)
{
if(DisplayWnd)
::SetWindowPos(DisplayWnd,HWND_TOP,0,0,Width,Height,SWP_NOACTIVATE);
TWinControl::Resize();
}
void __fastcall TMPlayer::CreateParams(TCreateParams &Params)
{
// clip children of this control
TWinControl::CreateParams(Params);
Params.Style |= WS_CLIPCHILDREN;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -