SOL9 Sample: WMPMediaEventsListener
|
1 Screenshot
2 Source code
/*
* WMPMediaEventsListener.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// 2011/12/08 Sample program to show properties of a Windows media
// by using SOL::MediaEventsListener.
#include <sol/Object.h>
#include <sol/ole/OleSite.h>
#include <sol/wmp/WMPMedia.h>
#include <sol/ole/MediaEventsListener.h>
#include <sol/ArgListT.h>
#include <sol/Locale.h>
namespace SOL {
#define WM_PLAY_FINISHED (WM_USER + 2011)
#define WM_PLAYING (WM_USER + 2012)
#define WM_ERROR (WM_USER + 2021)
//MediaProperty shows properties of a Windows media.
class WMPMediaEventsListener :public Object {
private:
//Local MediaEvents Listener class derived from SOL::MediaEventsListener
//-----------------------------------------------------------
//<InnerClass>
class __MediaEventsListener :public MediaEventsListener {
public:
//Constructor
__MediaEventsListener(DWORD thread)
:MediaEventsListener(thread)
{
}
public:
virtual void showProperties(HWND hwnd=NULL)
{
try {
IWMPPlayer4* pPlayer = getPlayer();
IWMPMedia* pMedia = NULL;
HRESULT hr = pPlayer->get_currentMedia(&pMedia);
if (FAILED(hr)) {
printf("Failed to get_currentMedia\n");
throw hr;
}
WMPMedia media = pMedia;
media.showProperties();
} catch (...) {
}
}
public:
virtual void __stdcall OpenStateChange (__in long NewState)
{
const TCHAR* name =getOpenStateName(NewState);
_tprintf(_T("__MediaEventsListener::OpenStateChange NewState = %s (%d)\n"), name, NewState);
}
public:
virtual void __stdcall PlayStateChange (__in long NewState)
{
const TCHAR* name =getPlayStateName(NewState);
_tprintf(_T("__MediaEventsListener::PlayStateChange NewState = %s (%d)\n"), name, NewState);
if (NewState == 1) {
//Playing has stopped, so post a user-defined message WM_PLAY_FINISHED
//to terminate a message loop in _tmain.
postThreadMessage(WM_PLAY_FINISHED, 0, 0);
}
}
public:
virtual void __stdcall Error()
{
printf("__MediaEventsListener::Error\n");
postThreadMessage(WM_ERROR, 0, 0);
// Stop a player by using a IWMPControls interface.
}
};
// </Innerclass>
//-----------------------------------------------------------
private:
IOleObject* oleObject;
IWMPPlayer4* pPlayer;
COleSite oleSite;
__MediaEventsListener mediaEvents;
public:
// Constructor
WMPMediaEventsListener()
:oleObject(NULL),
pPlayer(NULL),
mediaEvents(GetCurrentThreadId())
{
try {
HRESULT hr = CoCreateInstance( __uuidof(WindowsMediaPlayer), 0, CLSCTX_INPROC_SERVER,
IID_IOleObject, (void **)&oleObject);
if (FAILED(hr)) {
throw Exception("Failed to cocreateInstance\n", hr);
}
hr = oleObject->QueryInterface(__uuidof(IWMPPlayer), (void**)&pPlayer);
if (FAILED(hr)) {
throw Exception("Failed to queryInterface\n", hr);
}
hr = oleObject-> SetClientSite(&oleSite);
if (FAILED(hr)) {
throw Exception("SetClientSite failed\n", hr);
}
hr = mediaEvents.Advise(oleObject, __uuidof(IWMPEvents));
if (FAILED(hr)) {
throw Exception("Advise failed\n", hr);
}
IWMPSettings *pSetting = NULL;
hr = pPlayer->QueryInterface(__uuidof(IWMPSettings), (void **)&pSetting);
if (FAILED(hr)) {
throw Exception("QueryInterface to Setting failed\n", hr);
}
//set Auto-start
pSetting->put_autoStart(VARIANT_TRUE);
//set Silent- mode
//pSetting->put_mute(VARIANT_TRUE);
mediaEvents.putPlayer(pPlayer);
pSetting->Release();
} catch (Exception& ex) {
ex.printf();
} catch (HRESULT hr) {
}
}
public:
// Destructor
~WMPMediaEventsListener()
{
mediaEvents.Unadvise(oleObject, __uuidof(IWMPEvents));
if (oleObject){
oleObject->Release();
}
if (pPlayer) {
pPlayer->Release();
}
}
public:
void play(const TCHAR* file)
{
_bstr_t filename = file;
pPlayer->put_URL(filename);
}
public:
virtual void run(const TCHAR* file, HWND hwnd=NULL)
{
// Play a windows media for file.
play(file);
// Enter a typical windows-message-loop.
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
if (msg.message == WM_PLAY_FINISHED) {
//We got a message from MediaEventsListener class to termainte this message loop
break;
}
if (msg.message == WM_ERROR) {
IWMPControls* pControls = NULL;
HRESULT hr = pPlayer->QueryInterface(__uuidof(IWMPControls), (void **)&pControls);
if (SUCCEEDED(hr)) {
pControls->stop();
pControls->Release();
break;
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
};
}
// Console application
void _tmain(int argc, _TCHAR* argv[])
{
if (argc !=2) {
_tprintf(_T("Usage: %s MediaFile(*.mp3 or *.wmv or *.wma)\n"), argv[0]);
_tprintf(_T("Ex: %s chichibu.wmv\n"), argv[0]);
return;
}
Locale locale;
OleInitialize(NULL);
try {
WMPMediaEventsListener mediaEventsListener;
mediaEventsListener.run(argv[1]);
} catch (Exception& ex) {
ex.printf();
} catch (HRESULT hr) {
printf("Exception HRESULT=%x\n", hr);
} catch (...) {
}
OleUninitialize();
}
Last modified: 2 May 2016
Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.