播放列表

[与此页面关联的功能(Windows 媒体播放器 SDK)是旧版功能。 它已被 MediaPlayer 取代。 MediaPlayer 已针对Windows 10和Windows 11进行了优化。 如果可能,Microsoft 强烈建议新代码使用 MediaPlayer 而不是 Windows 媒体播放器 SDK。 如果可能,Microsoft 建议重写使用旧 API 的现有代码以使用新 API。]

Windows 媒体播放器 6.4 ActiveX 控件对象模型包括用于处理 Windows Media 图元文件播放列表的四种方法和一个属性:

  • Player6GetCurrentEntry
  • Player6SetCurrentEntry
  • Player6GetMediaParameter
  • Player6GetMediaParameterName
  • Player6EntryCount

它们共同提供有限的功能,用于导航具有 .asx 文件扩展名的播放列表图元文件和检索播放列表中包含的条目的相关信息。

Windows 媒体播放器 7 引入了“媒体库”。该库允许用户组织其数字媒体内容,以及创建可从 Player 图形用户界面管理的自定义播放列表。 Windows 媒体播放器 7 或更高版本的 ActiveX 控件对象模型支持使用库播放列表,以及 Windows Media 图元文件中包含的播放列表(文件扩展名为 .asx)。

注意

出于安全原因,用户必须授予对库的访问权限,程序才能操作其内容。 只能通过 Windows 媒体播放器 9 系列或更高版本的对象模型请求和授予访问权限。 有关访问权限的详细信息,请参阅 库访问

 

Windows 媒体播放器 7 或更高版本的对象模型包括三个用于处理播放列表的对象。 PlaylistCollection 对象提供用于组织播放列表的功能;它表示用户库中播放列表的整个集合。 PlaylistArray 对象提供了一种使用索引号从 PlaylistCollection 对象检索特定播放列表的方法;两个 PlaylistCollection 对象方法检索 PlaylistArray 对象。 Playlist 对象提供操作单个播放列表中包含的媒体项所需的属性和方法。

例如,由于库中的每个播放列表都有唯一的名称,因此可以使用 PlaylistCollection 从库中检索播放列表。getByName 方法:

// Retrieve a PlaylistArray object that contains 
// exactly one Playlist object.
var plarray = WMP9.playlistCollection.getByName("MyPlaylist");

// Get the Playlist object from the PlaylistArray object.
// The Playlist object has index number zero.
var pl = plarray.item(0);

// Make the retrieved playlist the current playlist.
WMP9.currentPlaylist = pl;

你最常希望使用当前播放列表。 虽然可以使用多个播放列表对象,但播放器只能检索其中一个。任何给定时间的 currentPlaylist 属性:Windows 媒体播放器正在处理的属性。

当 Windows 媒体播放器 7 或更高版本播放具有 .asx 文件扩展名的 Windows Media 图元文件时,它首先创建一个 Playlist 对象。 接下来,它将使用 .asx 播放列表中的信息填充 对象,然后将该 Playlist 对象设置为当前播放列表。 这意味着可以使用与 Playlist 对象关联的属性和方法操作 .asx 播放列表,就像处理库中的播放列表一样。 例如,若要使用版本 6.4 对象模型检索 .asx 播放列表中的条目数,请使用 Player6EntryCount 属性:

var entrycount = WMP64.EntryCount;

使用 Windows 媒体播放器 7 或更高版本的对象模型时,请使用播放列表count 属性:

var entrycount = WMP9.currentPlaylist.count;

使用版本 6.4 控件时,可以使用 Player6GetCurrentEntry 方法,用于检索 .asx 播放列表中当前条目的索引:

var entrynum = WMP64.GetCurrentEntry();

可以通过在脚本中使用 Windows 媒体播放器 7 或更高版本的对象模型来实现相同的结果。 以下 JScript 示例将当前媒体对象与播放列表中的每个项进行比较。 当媒体isIdentical 返回 true,消息框显示当前媒体项的索引。

function matchit(){
// Store the current playlist object in a variable.
var pl = WMP9.currentPlaylist;

// Loop through the playlist one entry at a time.
  for (var i = 0; i < pl.count; i++){

   // Test whether the current media item matches 
   // the item in the playlist at the current loop index.
   if (WMP9.currentmedia.isIdentical(pl.item(i))){

       // They match, display the index.
       var message = "Current media at index: " + i;
       alert(message);

       // Exit the function, don't continue looping!
       return;
      }
  }
}

若要指定 .asx 播放列表中当前条目的索引,请使用 Player6SetCurrentEntry。 版本 6.4 中的播放列表条目索引从 1 开始,因此若要使图元文件播放列表中的第二个条目成为当前项,请使用以下语法:

WMP6.SetCurrentEntry(2);

播放列表条目索引在 Windows 媒体播放器 7 或更高版本中从零开始;若要使图元文件播放列表中的第二个条目成为当前项,使用 Windows 媒体播放器 7 或更高版本的对象模型时,请使用以下语法:

WMP9.controls.currentItem = WMP9.currentPlaylist.item(1);

以下 JScript 示例演示了一个函数,该函数接受索引号作为参数,然后将对应于索引的播放列表条目设置为当前媒体项:

function setindex(idx){
// Store the current playlist in a variable.
var pl = WMP9.currentPlaylist;

// Get the first playlist entry.
var firstmedia = pl.item(0);

// Start the Player to allow navigation within the playlist.
WMP9.controls.playItem(firstmedia);

// Test whether idx is within a valid range.
   if (idx < pl.count && idx >= 0){

     // Set the currentItem to the desired playlist item.
     WMP9.controls.currentItem = pl.item(idx);

     // Display the name of the media item.
     alert(WMP9.currentMedia.name);
     return true;
 }

// The index is out of range, stop the Player, alert the user.
WMP9.controls.stop();
alert("Index out of range");
return false;
}

Windows Media 图元文件可以包含使用 <PARAM> 标记指定的自定义参数元素。 使用版本 6.4 对象模型时,可以使用 Player6 检索特定参数的名称。GetMediaParameterName 方法。 以下 JScript 示例检索 .asx 播放列表的第一个条目中第一个参数的名称:

var paramname = WMP6.GetMediaParameterName(1,1);

同样,可以使用 Player6 检索与 参数关联的值。GetMediaParameter

var paramvalue = WMP6.GetMediaParameter(1, paramname);

以下 JScript 示例使用 Windows 媒体播放器 7 或更高版本的对象模型从 .asx 播放列表中的第一个条目检索参数名称和值:

function getattribute(){
// Store the first playlist entry as a Media object.
var firstmedia = WMP9.currentplaylist.item(0);

// Get the name of the first parameter in the object named firstmedia.
var attname = firstmedia.getAttributeName(0);

// Get the value of the first parameter in the object named firstmedia.
var attval = firstmedia.getItemInfo(attname);

// Display the information.
alert(attname + ": " + attval);
}

可以使用 PlaylistCollectionimportPlaylist 方法,用于将 .asx 播放列表添加到库。 导入后,图元文件播放列表将成为库播放列表,因此你可以使用所有属性和方法进行操作。 用户必须授予对库的完全访问权限,应用程序才能使用 importPlaylist 方法。

可以使用 PlaylistCollectiongetByName 用于测试播放列表是否存在。 此方法始终返回有效的 PlaylistArray 对象。 如果检索到的播放列表数组正好包含一个播放列表,则库中存在具有该名称的播放列表。 否则,播放列表数组将不包含任何播放列表对象;这意味着库中没有具有作为参数传递给 getByName 方法的名称的播放列表。 以下 JScript 示例演示了这一点:

// Specify an .asx playlist file.
WMP9.URL = "https://www.microsoft.com/someplaylist.asx";

// Open the playlist and start playing the content.
WMP9.controls.play();

// Store the current playlist object.
var pl = WMP9.currentPlaylist;

// Attempt to retrieve from the library 
// a playlist having the same name as the current playlist.
var plarray = WMP9.playlistCollection.getByName(pl.name);

// Test whether the PlaylistArray object, plarray, contains
// a Playlist object.
if (!plarray.count)
   
   // If plarray contains no playlist, then import 
   // the current one.
   WMP9.playlistCollection.importPlaylist(pl);
}

管理播放列表

对象模型迁移指南

Playlist 对象