views:

4933

answers:

6

I want to use JavaScript to control an embedded Windows Media Player, as well as access any properties that the player exposes. I've found a few hacky examples online, but nothing concrete.

I really need access to play, pause, stop, seek, fullscreen, etc. I'd also like to have access to any events the player happens to broadcast.

Help would be wonderful (I already have a Flash equiv, just so you know), thanks!

+3  A: 

Windows media player is exposed as an activex control that any scripting language running in the windows script host should be able to access. You should be able to use jscript to control it. Jscript is microsofts implimentation of java script. For information on what objects and methods are availble using jscript for windows media player se this link.

Jared
A: 

There is no open JavaScript library as far as I know for crossbrowser clientside handling of a WMP player. However, this link should make it quite easy for you to start your own little library. The code might need some updating and testing in modern browser versions but you have the basics there.

The library your searching for would be a great idea for a Google Code project, I guess that while everyone today is using Adobe Flash with sIFR / swfobject or Microsoft Silverligt with sistr etc, there are not much interest to write clientside script controlling for WMP.

Sadly I have to deal with a big old corporate client whose IT dept thinks that adding Flash or Silverlight to the OS image might cause conflicts. Hooray for client work... thanks for the link though, it looks helpful.
ironkeith
A: 

http://www.schillmania.com/projects/soundmanager2/ allows you to access play, pause, etc but it is flash based.

Eric

epascarello
+3  A: 

There is an API in Microsoft's developer center, but it will only work if you embed windows media player using active-x.

To "learn" more about the API, check out MSDN: http://msdn.microsoft.com/en-us/library/dd564034(VS.85).aspx

ironkeith
The link is for Media Player in Microsoft Windows CE .NET 4.2I don't think that is the operating system you really care about
Ron Harlev
+4  A: 

The API requires ActiveX connectivity native to Internet Explorer, or can use a plugin for Firefox.

Here's a sample page that might get you started.

<html>
<head>
  <title>so-wmp</title>
  <script>

    onload=function() {
      player = document.getElementById("wmp");
      player.URL = "test.mp3";
    };

    function add(text) {
      document.body
        .appendChild(document.createElement("div"))
        .appendChild(document.createTextNode(text));
    };

    function handler(type) {
      var a = arguments;
      add(type +" = "+ PlayStates[a[1]]);
    };

    // http://msdn.microsoft.com/en-us/library/bb249361(VS.85).aspx
    var PlayStates = {
       0: "Undefined", // Windows Media Player is in an undefined state.
       1: "Stopped", // Playback of the current media item is stopped.
       2: "Paused", // Playback of the current media item is paused. When a media item is paused, resuming playback begins from the same location.
       3: "Playing", // The current media item is playing.
       4: "ScanForward", // The current media item is fast forwarding.
       5: "ScanReverse", // The current media item is fast rewinding.
       6: "Buffering", // The current media item is getting additional data from the server.
       7: "Waiting", // Connection is established, but the server is not sending data. Waiting for session to begin.
       8: "MediaEnded", // Media item has completed playback.
       9: "Transitioning", // Preparing new media item.
      10: "Ready", // Ready to begin playing.
      11: "Reconnecting" // Reconnecting to stream.
    };

  </script>
  <script for="wmp" event="PlayStateChange(newState)">
    // http://msdn.microsoft.com/en-us/library/bb249362(VS.85).aspx
    handler.call(this, "playstatechange", newState);
  </script>
</head>
<body>
  <div id="page">
    <object id="wmp"
       classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"
          type="application/x-oleobject">
    </object>
  </div>
</body>
</html>
Mister Lucky
A: 
dima