Share via


Loading a Playlist

Playlist files can be loaded and modified programmatically while content is being streamed to clients. You can modify playlist files by using the server object model and the XML Document Object Model (DOM). The following examples illustrate how to load a playlist and alter the paths to the media elements contained in it.

Visual Basic .NET Example

Imports Microsoft.WindowsMediaServices.Interop
Imports interop_msxml

Private Sub LoadPlylst()
  ' Declare variables.
  Dim Server As WMSServer
  Dim Playlist As IXMLDOMDocument
  Dim NodeList As IXMLDOMNodeList
  Dim ElementMedia As IXMLDOMElement
  Dim i As Integer
  Dim strPath As String

  Try
    ' Create a WMSServer object.
    Server = CreateObject("wmsserver.server")

    ' Create a new playlist.
    Playlist = Server.CreatePlaylist

    ' Load an existing playlist.
    Playlist.load("c:\wmpub\wmroot\playlist.wsx")

    ' Retrieve a list of all the media elements in the file.
    NodeList = Playlist.getElementsByTagName("media")

    ' Alter the path of each media element.
    For i = 0 To NodeList.length - 1

      ' Retrieve the next node in the list.
      ElementMedia = NodeList.item(i)

      ' Concatenate a new relative path to that of the
      ' current media element.
      strPath = "ASF\" + ElementMedia.getAttribute("src")

      ' Set the src attribute to contain the new relative path.
      ElementMedia.setAttribute("src", strPath)
    Next

    ' Save the playlist.
    Playlist.save("c:\wmpub\wmroot\playlist.wsx")

  Catch Err As Exception
    ' TODO: Exception handler goes here.
  Finally
    ' TODO: Clean-up code goes here.
  End Try

End Sub

C# Example

using Microsoft.WindowsMediaServices.Interop;
using interop_msxml;

// Declare variables.
WMSServer Server;
IXMLDOMDocument Playlist;
IXMLDOMNodeList NodeList;
IXMLDOMElement ElementMedia;
int i;
string strPath;

try
{
  // Create the WMSServer object.
  Server = new WMSServerClass();;

  // Create a new playlist.
  Playlist = Server.CreatePlaylist();

  // Load an existing playlist.
  Playlist.load("c:\\wmpub\\wmroot\\playlist.wsx");

  // Retrieve a list of all media elements in the playlist.
  NodeList = Playlist.getElementsByTagName("media");

  // Alter the path of each media element.
  for (i = 0; i < NodeList.length; i++)
  {
    // Retrieve the next node in the list.
    ElementMedia = (IXMLDOMElement)NodeList[i];

    // Concatenate a new relative path to that of the
    // current media element.
    strPath = "ASF\\" +  
    ElementMedia.getAttribute("src");

    // Set the src attribute to contain the new  
    // relative path.
    ElementMedia.setAttribute("src", strPath);
  }

  // Save the playlist.
  Playlist.save("c:\\wmpub\\wmroot\\playlist.wsx");
}

catch (Exception exc)
{
  // TODO: Exception handler goes here.
}

finally
{
  // TODO: Clean-up code goes here.
}

C++ Example

// Include header files.
#include <windows.h>
#include <atlbase.h>    // Includes CComBSTR and CComVariant.
#include "wmsserver.h"

// Declare variables and interfaces.
IWMSServer      *pServer;
IXMLDOMDocument *pPlaylist;
IXMLDOMElement  *pElementMedia;
IXMLDOMNode     *pNode;
IXMLDOMNodeList *pNodeList;

HRESULT         hr;
VARIANT_BOOL    bVal;
CComVariant     varFile;
CComBSTR        bstrName;
CComBSTR        bstrPath;
long            lCount;

// Initialize the COM library and retrieve a pointer
// to an IWMSServer interface.
hr = CoInitialize(NULL);
hr = CoCreateInstance(CLSID_WMSServer,
                      NULL,
                      CLSCTX_ALL,
                      IID_IWMSServer,
                      (void **)&pServer);
if (FAILED(hr)) goto EXIT;

// Create a new playlist.
hr = pServer->CreatePlaylist(&pPlaylist);
if (FAILED(hr)) goto EXIT;

// Load an existing playlist.
varFile = "c:\\wmpub\\wmroot\\playlist.wsx";
hr = pPlaylist->load(varFile, &bVal);
if (FAILED(hr)) goto EXIT;

// Retrieve a list of all the media elements in the file.
bstrName = "media";
hr = pPlaylist->getElementsByTagName(bstrName, &pNodeList);
if (FAILED(hr)) goto EXIT;

// Retrieve the total count of media elements.
hr = pNodeList->get_length(&lCount);
if (FAILED(hr)) goto EXIT;

// Alter the path of each media element.
for(long i = 0; i < lCount; i++)
{
    // Retrieve the next node in the list.
    hr = pNodeList->get_item(i, &pNode);
    if (FAILED(hr)) goto EXIT;

    // Query a pointer to the IXMLDOMElement interface.
    hr = pNode->QueryInterface(IID_IXMLDOMElement,
                              (void **)&pElementMedia);
    if (FAILED(hr)) goto EXIT;

    // Retrieve the src attribute for the element.
    bstrName = "src";
    hr = pElementMedia->getAttribute(bstrName, &varFile);
    if (FAILED(hr)) goto EXIT;

    // Concatenate a new relative path to that of the
    // current media element.
    bstrPath = "ASF\\";
    bstrPath += varFile.bstrVal;
    varFile.bstrVal = bstrPath;

    // Set the src attribute to contain the new relative path.
    hr = pElementMedia->setAttribute(bstrName, (VARIANT)varFile);
    if (FAILED(hr)) goto EXIT;

    // Release the objects.
    pNode->Release();
    pElementMedia->Release();
}

// Save the playlist.
varFile = "c:\\wmpub\\wmroot\\playlist.wsx";
hr = pPlaylist->save(varFile);
if (FAILED(hr)) goto EXIT;

EXIT:
    // TODO: Release temporary COM objects and uninitialize COM.

See Also (General)

See Also (Visual Basic .NET)

See Also (C#)

See Also (C++)