SOL9 Sample: WMPMediaProperty

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * WMPMediaProperty.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::WMPMediaProperty.


#include <sol/Object.h>
#include <sol/ole/OleSite.h>
#include <sol/wmp/WMPMedia.h>
#include <sol/wmp/WMPError.h>
#include <sol/wmp/WMPSettings.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)

//WMPMediaProperty shows properties of a Windows media.

class WMPMediaProperty :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 PlayStateChange (__in long NewState)
    {
      const TCHAR* name =getPlayStateName(NewState);
      _tprintf(_T("<PlayStateChange NewState =\"%s\">\n"), name);
      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);
      }

      if (NewState == 3) {
        //We are notified to be 'playing' a media.
        //Playing has started, so call showProperties method and post a user-defined message WM_PLAYING
        showProperties();
        postThreadMessage(WM_PLAYING, 0, 0);
      }
      _tprintf(_T("</PlayStateChange>\n"));

    }

  public:
    virtual void __stdcall Error() 
    {
      IWMPError* pError = NULL;
      IWMPPlayer4* pPlayer = getPlayer();
      //printf("__MediaEventsListener::Error\n");

      if (SUCCEEDED(pPlayer -> get_Error(&pError)) ) {
        WMPError error = pError;
        error.show();
      }
      postThreadMessage(WM_ERROR, 0, 0);  
    }
  };
  // </InnerClass> 
  //-----------------------------------------------------------

private:
  IOleObject*  oleObject;
  IWMPPlayer4*   pPlayer;

  COleSite oleSite;
  __MediaEventsListener mediaEvents;

public:
  // Constructor
  WMPMediaProperty()
    :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->get_settings(&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) {
      printf("Exception hr=%x\n", hr);
    }
  }

public:
  // Destructor
  ~WMPMediaProperty()
  {
    mediaEvents.Unadvise(oleObject, __uuidof(IWMPEvents));

    if (oleObject){
      oleObject->Release();
    }
    if (pPlayer) {
      pPlayer->Release();
    }
  }

public:
  void play(const TCHAR* file)
  {
    _bstr_t filename = file;
    HRESULT hr = pPlayer->put_URL(filename);
    if (FAILED(hr)) {
      _tprintf(_T("Failed to play %s\n"), file);
      throw hr;
    }
  }


public:
  virtual void display(const TCHAR* file, HWND hwnd=NULL)
  {
    // Play a windows media of a 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) {
        break;
      }

      if (msg.message == WM_PLAYING) {
        // Stop a player by using a IWMPControls interface.
        IWMPControls* pControls = NULL;
        HRESULT hr = pPlayer->QueryInterface(__uuidof(IWMPControls), (void **)&pControls);
        if (SUCCEEDED(hr)) {
          pControls->stop();
          pControls->Release();
        }
      }

      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) [optional xml-encoding]\n"), argv[0]);
    _tprintf(_T("Ex: %s chichibu.wmv ISO-8859-1\n"), argv[0]);
    return;
  }

  TCHAR encoding[1024];
  encoding[0] = '\0';
  if (argc ==3) {
    _stprintf_s(encoding, SizeOf(encoding), _T("encoding=\"%s\""), argv[2]); 
  }

  Locale locale;
  OleInitialize(NULL);
  try {
    _tprintf(_T("<?xml version=\"1.0\" %s?>\n"), encoding);
    _tprintf(_T("<%s>\n"), argv[0]);

    WMPMediaProperty mediaProperty;
    mediaProperty.display(argv[1]);
    _tprintf(_T("</%s>\n"), argv[0]);

  } 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.