SOL9 Sample: WMPNetworkProperty

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * WMPNetworkProperty.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2011/12/08 Sample program to show properties of a Windows media network
// by using SOL::WMPNetwork.


#include <sol/Object.h>
#include <sol/ole/OleSite.h>
#include <sol/wmp/WMPMedia.h>
#include <sol/wmp/WMPNetwork.h>
#include <sol/wmp/WMPError.h>

#include <sol/ole/MediaEventsListener.h>
#include <sol/ArgListT.h>
#include <sol/Locale.h>
#include <sol/FileWriter.h>


namespace SOL {

#define WM_PLAY_FINISHED (WM_USER + 2011)
#define WM_PLAYING       (WM_USER + 2012)
#define WM_MEDIAENDED    (WM_USER + 2013)
#define WM_TRANSIONING   (WM_USER + 2014)
#define WM_ERROR         (WM_USER + 2021)

//WMPNetworkProperty shows properties of a Windows media's network.

class WMPNetworkProperty :public Object {

private:
  //Local MediaEventsListener class derived from SOL::MediaEventsListner 
  //-----------------------------------------------------------
  //<InnerClass>
  class __MediaEventsListener :public MediaEventsListener {
  private:
    Writer* writer;

  public:
    //Constructor
    __MediaEventsListener(DWORD thread)
      :MediaEventsListener(thread)
    {
    }

  public:
    void putWriter(Writer* writer)
    {
      this->writer = writer;
    }

  public:
    virtual void writeProperties(Writer* writer)
    {
      try {
        IWMPPlayer4* pPlayer = getPlayer();
        IWMPNetwork* pNetwork = NULL;
        HRESULT hr = pPlayer->get_network(&pNetwork);
        if (FAILED(hr)) {
          printf("Failed to get_network\n");
          throw hr;
        }

        WMPNetwork network = pNetwork;
        network.writeProperties(*writer);
        //printf("\n");
      } catch (...) {
      }
    }

  public:
    virtual void __stdcall PlayStateChange (__in long NewState)
    {
      const TCHAR* name =getPlayStateName(NewState);
      writer->writeln(L"<PlayStateChange NewState=\"%s\">", 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
        writeProperties(writer);
        postThreadMessage(WM_PLAYING, 0, 0);
      }
      if (NewState == 8) {
        //We are notified to be 'MediaEnded' for a current media.
        writeProperties(writer);
        postThreadMessage(WM_MEDIAENDED, 0, 0);
      }
      if (NewState == 9) {
        //We are notified to be 'Transitioning'.
        writeProperties(writer);
      }
      writer->writeln(L"</PlayStateChange>");

    }
  public:
    virtual void __stdcall Error() 
    {
      IWMPError* pError = NULL;
      IWMPPlayer4* pPlayer = getPlayer();

      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
  WMPNetworkProperty(Writer* writer)
    :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.putWriter(writer);
      mediaEvents.putPlayer(pPlayer);

      pSetting->Release();

    } catch (Exception& ex) {
      ex.printf();
    } catch (HRESULT hr) {

    }
  }

public:
  // Destructor
  ~WMPNetworkProperty()
  {
    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 display(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) {
        break;
      }

      if (msg.message == WM_PLAYING) {
        // Stop a play 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 != 3) {
    _tprintf(_T("Usage: %s url xmlFile\n"), argv[0]);
    _tprintf(_T("Ex: %s http://www.antillia.com/chichibu/fudasho/fudasho_1/shimabuji_fall_1122.wmv sample.xml\n"), argv[0]);
    return;
  }

  _bstr_t url = argv[1];
  const TCHAR* xmlFile = argv[2];

  Locale locale;

  OleInitialize(NULL);
  try {
    FileWriter writer = xmlFile;

    writer.writeln(L"<?xml version=\"1.0\" encoding=\"UTF-16\"?>");
    writer.writeln(L"<%s>", argv[0]);

    WMPNetworkProperty networkProperty(&writer);
    networkProperty.display(argv[1]);

    writer.writeln(L"</%s>", 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.