I am trying to play a movie using Windows Media Player, later on will add some other functionality. Following is the code I have written:
const CLSID CLSID_WindowsMediaPlayer = {0x6BF52A52, 0x394A, 0x11d3, {0xB1, 0x53, 0x00, 0xC0, 0x4F, 0x79, 0xFA, 0xA6 } };
HRESULT hr;
IWMPPlayer *pMediaPlayer = NULL;
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if(FAILED(hr))
{
std::cout << "ERR -- Could not Initialize COM engine for you" << std::endl;
return 0;
}
hr = CoCreateInstance(CLSID_WindowsMediaPlayer, NULL, CLSCTX_INPROC_SERVER, IID_IDispatch, (void**)&pMediaPlayer);
if(FAILED(hr))
{
std::cout << "ERR - Could not get WMPPlayer Interface Pointer" << std::endl;
return 0;
}
std::cout << "Got MediaPlayer Pointer" << std::endl;
IWMPSettings *pMediaPlayerSettings = NULL;
hr = pMediaPlayer->get_settings(&pMediaPlayerSettings);
if(FAILED(hr))
{
std::cout << "ERR - Could not get WMPSettings Interface Pointer" << std::endl;
ReleaseInterfaces((IUnknown**)&pMediaPlayer);
return 0;
}
std::cout << "Got MediaPlayerSettings Pointer" << std::endl;
hr = pMediaPlayerSettings->put_autoStart(VARIANT_TRUE);
if(FAILED(hr))
{
std::cout << "ERR - Could not put auto_start to true" << std::endl;
ReleaseInterfaces((IUnknown**)&pMediaPlayerSettings);
ReleaseInterfaces((IUnknown**)&pMediaPlayer);
return 0;
}
std::cout << "Have put it to autostart" << std::endl;
hr = pMediaPlayerSettings->put_volume(50);
if(FAILED(hr))
{
std::cout << "ERR - Could not put volume" << std::endl;
ReleaseInterfaces((IUnknown**)&pMediaPlayerSettings);
ReleaseInterfaces((IUnknown**)&pMediaPlayer);
return 0;
}
std::cout << "Have put volume to listen-able" << std::endl;
hr = pMediaPlayer->put_URL(L"C:\\background.mp3");
if(FAILED(hr))
{
std::cout << "ERR - Could not set URL" << std::endl;
ReleaseInterfaces((IUnknown**)&pMediaPlayerSettings);
ReleaseInterfaces((IUnknown**)&pMediaPlayer);
return 0;
}
std::cout << "Have set URL" << std::endl;
So far all is good. But file is never played. Upon further investigation, I found out that WMPPlayState
never becomes wmppsPlaying
, So i tested if file is opened using WMPOpenState
but here I always get wmposOpeningUnknownURL
. I first thought this might be because I have put file in C:
, which needs admin rights, but using other location also yields same result. I have checked if the URL i set using put_URL
is actually put, and yes, get_URL
gives out my set url. I have also tested with different files and formats.
Moreover, Windows Media Player is NOT opened!