Inserting Playlist Entries
The following examples illustrate how to create a playlist and add multiple elements and attributes to it. You must create a new IXMLDOMElement object for every new element added. Note that the server adds attributes to the playlist object in the reverse of the order in which they appear in the code.
Visual Basic .NET Example
Imports Microsoft.WindowsMediaServices.Interop
Imports interop_msxml
Private Sub InsertEntry()
' Declare variables.
Dim Server As WMSServer
Dim Playlist As IXMLDOMDocument
Dim ElementSmil As IXMLDOMElement
Dim ElementMedia As IXMLDOMElement
Dim ProcInst As IXMLDOMNode
Dim Root As IXMLDOMNode
Dim Node As IXMLDOMNode
Try
' Create new WMSServer and IWMSPlaylist objects.
Server = New WMSServer()
Playlist = Server.CreatePlaylist
' Create the processing instruction node.
ProcInst = Playlist.createNode( _
DOMNodeType.NODE_PROCESSING_INSTRUCTION, _
"wsx", _
"")
' Add the processing instruction to the file structure.
Playlist.appendChild(ProcInst)
ProcInst.text = "version = '1.0'"
' Create and add the root node of the playlist.
ElementSmil = Playlist.createElement("smil")
Root = Playlist.appendChild(ElementSmil)
' Create a media element for the playlist and add a
' src attribute to it.
ElementMedia = Playlist.createElement("media")
ElementMedia.setAttribute("src", "c:\wmpub\wmroot\ad1.wmv")
Node = Root.appendChild(ElementMedia)
' Create another media element and add multiple attributes to it.
' The server adds attributes to an element in the reverse of the
' order in which they appear in the code.
ElementMedia = Playlist.createElement("media")
ElementMedia.setAttribute("dur", "+.5min")
ElementMedia.setAttribute("clipBegin", "0:00")
ElementMedia.setAttribute("src", "c:\wmpub\wmroot\movie.wmv")
Node = Root.appendChild(ElementMedia)
' Create another media element and add a src attribute to it.
ElementMedia = Playlist.createElement("media")
ElementMedia.setAttribute("src", "c:\wmpub\wmroot\ad2.wmv")
Node = Root.appendChild(ElementMedia)
' 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;
try
{
// Declare the playlist, element, and node variables.
IXMLDOMDocument playlist;
IXMLDOMElement element_smil, element_media;
IXMLDOMNode proc_inst, root, node;
// Create new server and playlist objects.
WMSServer server = new WMSServerClass();
playlist = (IXMLDOMDocument)server.CreatePlaylist();
// Create the processing instruction node.
proc_inst = playlist.createNode(DOMNodeType.NODE_PROCESSING_INSTRUCTION,
"wsx", "");
// Add the processing instruction to the file structure.
playlist.appendChild(proc_inst);
proc_inst.text = "version = 1.0";
// Create and add the root node of the playlist.
element_smil = playlist.createElement("smil");
root = playlist.appendChild(element_smil);
// Create a media element and assign a src attribute.
element_media = playlist.createElement("media");
element_media.setAttribute("src", "c:\\wmpub\\wmroot\\ad1.wmv");
node = root.appendChild(element_media);
// Create another media element and add multiple attributes to
// it. The server adds attributes to an element in the reverse
// of the order in which they appear in the code.
element_media = playlist.createElement("media");
element_media.setAttribute("dur", "+.5min");
element_media.setAttribute("clipBegin", "0:00");
element_media.setAttribute("src", "c:\\wmpub\\wmroot\\movie.wmv");
node = root.appendChild(element_media);
// Create another media element and assign a src attribute.
element_media = playlist.createElement("media");
element_media.setAttribute("src", "c:\\wmpub\\wmroot\\ad2.wmv");
node = root.appendChild(element_media);
// Save the playlist.
playlist.save("c:\\wmpub\\wmroot\\playlist.wsx");
}
catch (Exception)
{
// 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 *pElement_Smil, *pElement_Media;
IXMLDOMNode *pProc_Inst, *pRoot, *pNode;
CComBSTR bstrName, bstrNamespaceURI, bstrVersion;
CComVariant varPath, varBegin, varDuration;
HRESULT hr;
// Initialize the COM library and retrieve a pointer
// to an IWMSServer interface.
hr = CoInitialize(NULL);
if (FAILED(hr))goto EXIT;
hr = CoCreateInstance(CLSID_WMSServer,
NULL,
CLSCTX_ALL,
IID_IWMSServer,
(void **)&pServer);
if (FAILED(hr))goto EXIT;
// Create the playlist object.
hr = pServer->CreatePlaylist(&pPlaylist);
if (FAILED(hr))goto EXIT;
// Create the processing instruction node.
bstrName = "wsx";
bstrNamespaceURI = "";
hr = pPlaylist->createNode((CComVariant)NODE_PROCESSING_INSTRUCTION,
bstrName,
bstrNamespaceURI,
&pProc_Inst);
if (FAILED(hr))goto EXIT;
// Add the processing instruction to the file structure.
hr = pPlaylist->appendChild(pProc_Inst, &pNode);
if (FAILED(hr))goto EXIT;
bstrVersion = "version = 1.0";
pProc_Inst->put_text(bstrVersion);
// Create and add the root node of the playlist.
bstrName = "smil";
hr = pPlaylist->createElement(bstrName, &pElement_Smil);
if (FAILED(hr))goto EXIT;
hr = pPlaylist->appendChild(pElement_Smil, &pRoot);
if (FAILED(hr))goto EXIT;
// Create a media element and add a src attribute.
bstrName = "media";
hr = pPlaylist->createElement(bstrName, &pElement_Media);
if (FAILED(hr))goto EXIT;
bstrName = "src";
varPath = "c:\\wmput\\wmroot\\moview.wmv";
hr = pElement_Media->setAttribute(bstrName, varPath);
if (FAILED(hr))goto EXIT;
// Add the media element to the root node.
hr = pRoot->appendChild(pElement_Media, &pNode);
if (FAILED(hr))goto EXIT;
// Create a second media element and assign multiple
// attributes. Note that the server reverses the order
// of the attributes (from how they appear in the code).
bstrName = "media";
hr = pPlaylist->createElement(bstrName, &pElement_Media);
if (FAILED(hr))goto EXIT;
bstrName = "dur";
varDuration = "+.5min";
hr = pElement_Media->setAttribute(bstrName, varDuration);
if (FAILED(hr))goto EXIT;
bstrName = "clipBegin";
varBegin = "0:00";
hr = pElement_Media->setAttribute(bstrName, varBegin);
if (FAILED(hr))goto EXIT;
bstrName = "src";
varPath = "c:\\wmput\\wmroot\\movie.wmv";
hr = pElement_Media->setAttribute(bstrName, varPath);
if (FAILED(hr))goto EXIT;
// Add the media element to the root node.
hr = pRoot->appendChild(pElement_Media, &pNode);
if (FAILED(hr))goto EXIT;
// Create a third media element and add a src attribute.
bstrName = "media";
hr = pPlaylist->createElement(bstrName, &pElement_Media);
if (FAILED(hr))goto EXIT;
bstrName = "src";
varPath = "c:\\wmput\\wmroot\\ad2.wmv";
hr = pElement_Media->setAttribute(bstrName, varPath);
if (FAILED(hr))goto EXIT;
// Add the media element to the root node.
hr = pRoot->appendChild(pElement_Media, &pNode);
if (FAILED(hr))goto EXIT;
// Save the playlist.
varPath = "c:\\wmpub\\wmroot\\playlist.wsx";
hr = pPlaylist->save(varPath);
if (FAILED(hr))goto EXIT;
EXIT:
// TODO: Release temporary objects and uninitialize COM.
The preceding examples create the following playlist.
<?wsx version = '1.0' ?>
<smil>
<media src="c:\wmpub\wmroot\ad1.wmv"/>
<media src="c:\wmpub\wmroot\movie.wmv" clipBegin="0:00" dur="+.5min" />
<media src="c:\wmpub\wmroot\ad2.wmv"/>
</smil>
See Also (General)
See Also (Visual Basic .NET)
IWMSServerIWMSServer Object (Visual Basic .NET)
IXMLDOMDocumentIXMLDOMDocument Object (Visual Basic .NET)
IXMLDOMElementIXMLDOMElement Object (Visual Basic .NET)
IXMLDOMNodeIXMLDOMNode Object (Visual Basic .NET)
See Also (C#)
IWMSServerIWMSServer Object (C#)
IXMLDOMDocumentIXMLDOMDocument Object (C#)
IXMLDOMElementIXMLDOMElement Object (C#)
IXMLDOMNodeIXMLDOMNode Object (C#)
See Also (C++)
IWMSServerIWMSServer Interface
IXMLDOMDocumentIXMLDOMDocument Interface
IXMLDOMElementIXMLDOMElement Interface
IXMLDOMNodeIXMLDOMNode Interface