Compartir a través de


Creating a Wrapper Playlist

Wrapper playlists allow additional content, such as advertising, to be streamed to clients before and/or after streaming the requested content. You can modify playlist files by using the server object model and the XML Document Object Model (DOM). The following examples illustrate how to create a simple wrapper playlist and assign all publishing points to point to it.

Visual Basic .NET Example

Imports Microsoft.WindowsMediaServices.Interop
Imports interop_msxml

Private Sub CreateWrapper()

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

  Try

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

    ' Create a new playlist.
    Playlist = Server.CreatePlaylist

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

    ' Add the file type version number to the node.
    NodeProcInst.text = "version = '1.0'"

    ' Add the processing instruction to the playlist.
    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 src attribute for the media element.
    ElementMedia.setAttribute("src", "beginning_advertisement.wmv")

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

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

    ' Set the src attribute for the media element to
    ' point to the URL requested by the client.
    ElementMedia.setAttribute("src", "%RequestedURL%")

    ' 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 src attribute for the media element.
    ElementMedia.setAttribute("src", "trailing_advertisement.wmv")

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

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

    ' Retrieve the IWMSPublishingPoints object.
    PubPoints = Server.PublishingPoints

    ' Retrieve each publishing point and set the wrapper
    ' path to point to the newly created wrapper playlist.
    For i = 0 To PubPoints.Count - 1
      PubPoints.Item(i).WrapperPath = "c:\wmpub\wmroot\wrapper.wsx"
      PubPoints.Item(i).EnableWrapperPath = True
    Next

  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;
IWMSPublishingPoints PubPoints;
IXMLDOMDocument Playlist;
IXMLDOMElement ElementSmil;
IXMLDOMElement ElementMedia;
IXMLDOMNode NodeProcInst;
IXMLDOMNode Root;
IXMLDOMNode Node;
int i;

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(
                       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 src attribute for the media element.
  ElementMedia.setAttribute("src",  "beginning_advertisement.wmv");

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

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

  // Set the src attribute for the media element to
  // point to the URL requested by the client.
  ElementMedia.setAttribute("src", "%RequestedURL%");;

  // 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 src attribute for the media element.
  ElementMedia.setAttribute("src",  "trailing_advertisement.wmv");

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

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

  // Retrieve the IWMSPublishingPoints object.
  PubPoints = Server.PublishingPoints;

  // Retrieve each publishing point and set the wrapper
  // path to point to the newly created wrapper playlist.
  for (i=0; i<PubPoints.Count; i++)
  {
    PubPoints[i].WrapperPath =  "c:\\wmpub\\wmroot\\wrapper.wsx";
    PubPoints[i].EnableWrapperPath = true;
  }
}

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;
IWMSPublishingPoints *pPubPoints;
IWMSPublishingPoint  *pPubPoint;
IXMLDOMDocument      *pPlaylist;
IXMLDOMElement       *pElementSmil;
IXMLDOMElement       *pElementMedia;
IXMLDOMNode          *pNodeProcInst;
IXMLDOMNode          *pRoot;
IXMLDOMNode          *pNode;

HRESULT         hr;
CComVariant     varIndex;
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;

// 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 src attribute for the media element.
bstrName = "src";
varFile = "beginning_advertisement.wmv";
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 a media element for the playlist.
bstrName = "media";
hr = pPlaylist->createElement(bstrName, &pElementMedia);
if (FAILED(hr)) goto EXIT;

// Set the src attribute for the media element to
// point to the URL requested by the client.
bstrName = "src";
varFile = "%RequestedURL%";
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 src attribute for the media element.
bstrName = "src";
varFile = "trailing_advertisement.wmv";
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 = "c:\\wmpub\\wmroot\\wrapper.wsx";
hr = pPlaylist->save(varFile);
if (FAILED(hr)) goto EXIT;

// Retrieve a pointer to the IWMSPublishingPoints
// interface and retrieve the number of publishing
// points.
hr = pServer->get_PublishingPoints(&pPubPoints);
if (FAILED(hr)) goto EXIT;
hr = pPubPoints->get_Count(&lCount);
if (FAILED(hr)) goto EXIT;

// Retrieve each publishing point and set the wrapper
// path to point to the newly created wrapper playlist.
for (int x = 0; x < lCount; x++)
{
    varIndex = x;
    hr = pPubPoints->get_Item(varIndex, &pPubPoint);
    if (FAILED(hr)) goto EXIT;
    hr = pPubPoint->put_WrapperPath(varFile.bstrVal);
    if (FAILED(hr)) goto EXIT;
    hr = pPubPoint->put_EnableWrapperPath(VARIANT_TRUE);
    if (FAILED(hr)) goto EXIT;

    // Release the IWMSPublishingPoint object.
    pPubPoint->Release();
}

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

See Also (General)

See Also (Visual Basic .NET)

See Also (C#)

See Also (C++)