Setting Durations

Playlist files can contain media elements that play for a set length of time and then stop. The attributes associated with the media elements enable you to control exactly how much a client sees when streaming content. You can modify playlist files by using the server object model and the XML Document Object Model (DOM). For more information, see XML DOM Interfaces (C++). The following examples illustrate how to create a simple playlist containing two media elements with set play durations.

Visual Basic .NET Example

Imports Microsoft.WindowsMediaServices.Interop
Imports interop_msxml
Imports System.Runtime.InteropServices

' Declare variables.
Dim Server As WMSServer
Dim Playlist As IXMLDOMDocument
Dim ElementSmil As IXMLDOMElement
Dim ElementMedia As IXMLDOMElement
Dim NodeProcInst As IXMLDOMNode
Dim Root As IXMLDOMNode
Dim Node As IXMLDOMNode

Try
    ' Create the WMSServer object.
    Server = New WMSServer()

    ' Create a new playlist.
    Playlist = Server.CreatePlaylist

    ' Create the processing instruction node
    ' to indicate the file type.
    NodeProcInst = Playlist.createNode(interop_msxml.DOMNodeType.NODE_PROCESSING_INSTRUCTION, _
                                       "wsx", _
                                       vbNullString)

    ' Add the file type version number to the node.
    NodeProcInst.text = "version=" & Chr(34) & "1.0" & Chr(34)

    ' Add the processing instruction to the file structure.
    Playlist.appendChild(NodeProcInst)

    ' Create the root node of the playlist.
    ElementSmil = Playlist.createElement("smil")

    ' Add the root node to the file structure.
    Root = Playlist.appendChild(ElementSmil)

    ' Create a media element for the playlist.
    ElementMedia = Playlist.createElement("media")

    ' Set the duration attribute to 30 seconds.
    ElementMedia.setAttribute("dur", "30s")

    ' Set the src attribute for the media element.
    ElementMedia.setAttribute("src", "welcome1.asf")

    ' Add the media element under the root node in
    ' the file structure.
    Node = Root.appendChild(ElementMedia)

    ' Create another media element for the playlist.
    ElementMedia = Playlist.createElement("media")

    ' Set the duration attribute to 2 minutes.
    ElementMedia.setAttribute("dur", "2min")

    ' Set the src attribute for the media element.
    ElementMedia.setAttribute("src", "welcome3.asf")

    ' Add the media element under the root node in
    ' the file structure.
    Node = Root.appendChild(ElementMedia)

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

Catch errCom As COMException
    ' TODO: Handle COM exceptions.
Catch err As Exception
    ' TODO: Exception handler goes here.
Finally
    ' TODO: Clean-up code goes here.
End Try

C# Example

using Microsoft.WindowsMediaServices.Interop;
using interop_msxml;
using System.Runtime.InteropServices;

WMSServer Server;
IXMLDOMDocument Playlist;
IXMLDOMElement ElementSmil;
IXMLDOMElement ElementMedia;
IXMLDOMNode NodeProcInst;
IXMLDOMNode Root;
IXMLDOMNode Node;

try
{
    // Create the WMSServer object.
    Server = new WMSServerClass();;
 
    // Create a new playlist.
    Playlist = Server.CreatePlaylist();

    // Create the processing instruction node
    // to indicate the file type.
    NodeProcInst = Playlist.createNode(interop_msxml.DOMNodeType.NODE_PROCESSING_INSTRUCTION,
                                        "wsx",
                                        null);

    // Add the file type version number to the node.
    NodeProcInst.text = "version=" + "\"" + "1.0" + "\"";

    // Add the processing instruction to the file structure.
    Playlist.appendChild(NodeProcInst);

    // Create the root node of the playlist.
    ElementSmil = Playlist.createElement("smil");

    // Add the root node to the file structure.
    Root = Playlist.appendChild(ElementSmil);

    // Create a media element for the playlist.
    ElementMedia = Playlist.createElement("media");

    // Set the duration attribute to 30 seconds.
    ElementMedia.setAttribute("dur", "30s");

    // Set the src attribute for the media element.
    ElementMedia.setAttribute("src", "welcome1.asf");

    // Add the media element under the root node in
    // the file structure.
    Node = Root.appendChild(ElementMedia);

    // Create another media element for the playlist.
    ElementMedia = Playlist.createElement("media");

    // Set the duration attribute to 2 minutes.
    ElementMedia.setAttribute("dur", "2min");

    // Set the src attribute for the media element.
    ElementMedia.setAttribute("src", "welcome3.asf");

    // Add the media element under the root node in
    // the file structure.
    Node = Root.appendChild(ElementMedia);

    // Save the playlist.
    Playlist.save("c:\\wmpub\\wmroot\\playlist.wsx");
}
catch (COMException comExc)
{
    // TODO: Handle COM exceptions.
}
catch (Exception exc)
{
    // TODO: Exception handler goes here.
}
finally
{
    // TODO: Clean-up code goes here.
}

C++ Example

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

// Declare variables and interfaces.
IWMSServer      *pServer;
IXMLDOMDocument *pPlaylist;
IXMLDOMElement  *pElementSmil;
IXMLDOMElement  *pElementMedia;
IXMLDOMNode     *pNodeProcInst;
IXMLDOMNode     *pRoot;
IXMLDOMNode     *pNode;

HRESULT         hr;
CComVariant     varFile;
CComBSTR        bstrName;

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

// Create the processing instruction node
// to indicate the file type.
bstrName = "wsx";
varFile = NODE_PROCESSING_INSTRUCTION;
hr = pPlaylist->createNode(varFile, bstrName, NULL, &pNodeProcInst);
if (FAILED(hr)) goto EXIT;

// Add the file type version number to the node.
bstrName = "version=\"1.0\"";
hr = pNodeProcInst->put_text(bstrName);
if (FAILED(hr)) goto EXIT;

// Add the processing instruction to the file structure.
hr = pPlaylist->appendChild(pNodeProcInst, &pRoot);
if (FAILED(hr)) goto EXIT;

// Create the root node of the playlist.
bstrName = "smil";
hr = pPlaylist->createElement(bstrName, &pElementSmil);
if (FAILED(hr)) goto EXIT;

// Add the root node to the file structure.
hr = pPlaylist->appendChild(pElementSmil, &pRoot);
if (FAILED(hr)) goto EXIT;

// Create a media element for the playlist.
bstrName = "media";
hr = pPlaylist->createElement(bstrName, &pElementMedia);
if (FAILED(hr)) goto EXIT;

// Set the duration attribute to 30 seconds.
bstrName = "dur";
varFile = "30s";
hr = pElementMedia->setAttribute(bstrName, varFile);
if (FAILED(hr)) goto EXIT;

// Set the src attribute for the media element.
bstrName = "src";
varFile = "welcome1.asf";
hr = pElementMedia->setAttribute(bstrName, varFile);
if (FAILED(hr)) goto EXIT;

// Add the media element under the root node in
// the file structure.
hr = pRoot->appendChild(pElementMedia, &pNode);
if (FAILED(hr)) goto EXIT;

// Create another media element for the playlist.
bstrName = "media";
hr = pPlaylist->createElement(bstrName, &pElementMedia);
if (FAILED(hr)) goto EXIT;

// Set the duration attribute to 2 minutes for the
// media element.
bstrName = "dur";
varFile = "2min";
hr = pElementMedia->setAttribute(bstrName, varFile);
if (FAILED(hr)) goto EXIT;

// Set the src attribute for the media element.
bstrName = "src";
varFile = "welcome3.asf";
hr = pElementMedia->setAttribute(bstrName, varFile);
if (FAILED(hr)) goto EXIT;

// Add the media element under the root node in
// the file structure.
hr = pRoot->appendChild(pElementMedia, &pNode);
if (FAILED(hr)) goto EXIT;

// Save the playlist.
varFile = "file://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++)