是否有针对Windows Media Player的文档化JavaScript API?

时间:2008-11-18 17:57:53

标签: javascript windows-media-player

我想使用JavaScript来控制嵌入式Windows Media Player,以及访问播放器公开的任何属性。我在网上找到了一些hacky示例,但没有具体的内容。

我真的需要访问播放,暂停,停止,搜索,全屏等。我还希望能够访问播放器正在播放的任何事件。

帮助会非常棒(我已经有了Flash等效,只是你知道),谢谢!

5 个答案:

答案 0 :(得分:11)

API需要Internet Explorer本机的ActiveX连接,或者可以使用plugin for Firefox

这是一个可以帮助您入门的示例页面。

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

答案 1 :(得分:6)

Microsoft的开发人员中心有一个API,但只有在使用active-x嵌入Windows Media Player时才能使用。

要“了解”有关API的更多信息,请查看MSDN:http://msdn.microsoft.com/en-us/library/dd564034(VS.85).aspx

答案 2 :(得分:4)

Windows媒体播放器作为activex控件公开,Windows脚本宿主中运行的任何脚本语言都应该能够访问。您应该能够使用jscript来控制它。 Jscript是java脚本的微软实现。有关使用jscript for windows media player se this link.

可以使用哪些对象和方法的信息

答案 3 :(得分:0)

据我所知,对于浏览器客户端处理WMP播放器,没有开放的JavaScript库。 但是,this link应该可以让您轻松启动自己的小型库。代码可能需要在现代浏览器版本中进行一些更新和测试,但您有基础知识。

您搜索的图书馆对于Google Code项目来说是一个好主意,我想今天所有人都在使用带有sIFR / swfobject的Adobe Flash或带有sistr的Microsoft Silverligt等,为WMP编写客户端脚本控制没什么兴趣。

答案 4 :(得分:0)

应使用下一个WMP对象(适用于Chrome,FF,Safari)

    objPlayer = document.getElementById("wmp");           
    objPlayer.controls.stop();
    objPlayer.URL = this.url;
    objPlayer.controls.play();

<EMBED id="wmp" TYPE="application/x-mplayer2" name="MediaPlayer" width="0" height="0" ShowControls="0" ShowStatusBar="0" ShowDisplay="0" autostart="0"></EMBED>
相关问题