Inserting Ads Based on Sort Criteria
You can use the role attribute to filter content in a playlist so that advertisements can be inserted at specific locations. The following example code illustrates how to load a playlist and programmatically insert an ad after every second song. Assume that the publishing point uses the following playlist.
<?wsx version="1.0"?>
<smil>
<media src="c:\wmpub\wmroot\Song_1.wma" role="song" />
<media src="c:\wmpub\wmroot\Song_2.wma" role="song" />
<media src="c:\wmpub\wmroot\Presentation_Intro.wmv" role="intro" />
<media src="http://encoder_name:80" role="presentation" />
<media src="c:\wmpub\wmroot\Song_3.wma" role="song" />
<media src="c:\wmpub\wmroot\Song_4.wma" role="song" />
</smil>
To add an advertisement after every second song, use code similar to the following.
Visual Basic .NET Example
Imports Microsoft.WindowsMediaServices.Interop
Imports interop_msxml
Private Sub InsertAds()
' Declare variables.
Dim Server As WMSServer
Dim Playlist As IWMSPlaylist
Dim Ad_Elements() As IXMLDOMElement
Dim Root_Node As IXMLDOMElement
Dim New_Element As IXMLDOMElement
Dim NodeList As IXMLDOMNodeList
Dim Attr As IXMLDOMNode
Dim Song_Child As IXMLDOMNode
Dim Next_Child As IXMLDOMNode
Dim lIndex As Long
Dim lSongCounter As Long
Dim lAdNumber As Long
Try
' Create the WMSServer object.
Server = CreateObject("wmsserver.server")
' Load an existing playlist.
Playlist = Server.CreatePlaylist
Playlist.load("c:\wmpub\wmroot\playlist.wsx")
' Find the root node. The root node of a server-side
' playlist must be the smil element.
Root_Node = Playlist.documentElement
' Retrieve a list of the media elements in the smil
' time container. In this example, all child elements of
' the root node are media elements.
NodeList = Root_Node.getElementsByTagName("media")
' Create a media element for the playlist and identify
' it as an advertisement by setting the role attribute
' to "Advertisement". This enables logging plug-ins to log
' information about the ads.
ReDim Ad_Elements(0)
Ad_Elements(0) = Playlist.createElement("media")
Ad_Elements(0).setAttribute("role", "Advertisement")
Ad_Elements(0).setAttribute("src", "c:\wmpub\wmroot\Ad_1.wmv")
Ad_Elements(0).setAttribute("id", "media_advertisement")
' Initialize variables to count the number of songs and ads.
lSongCounter = 0
lAdNumber = 1
' Iterate through the list to find the songs. In this example,
' a song is identified by a role attribute equal to "song".
For lIndex = 0 To NodeList.length - 1
' Retrieve the role attribute for each media element
' and check to see whether it equals "song".
Attr = NodeList.item(lIndex).attributes.getNamedItem("role")
If Attr.nodeValue = "song" Then
lSongCounter = lSongCounter + 1
End If
' Insert an ad after two songs.
If (lSongCounter <> 0) And (lSongCounter Mod 2 = 0) Then
' Clone Ad_Elements(0) to add the same advertisement
' each time.
ReDim Preserve Ad_Elements(lAdNumber)
Ad_Elements(lAdNumber) = Ad_Elements(0).cloneNode(False)
' Retrieve the media element associated with the role attribute.
Song_Child = NodeList.item(lIndex)
' Find the next child node.
If Not Song_Child Is Root_Node.lastChild Then
Next_Child = Song_Child.nextSibling
End If
' Insert the ad in front of the next child node. If the song
' is the last media element in the playlist, then append the ad.
If Not Song_Child Is Root_Node.lastChild Then
New_Element = Root_Node.insertBefore(Ad_Elements(lAdNumber), _
Next_Child)
Else
New_Element = Root_Node.appendChild(Ad_Elements(lAdNumber))
End If
lAdNumber = lAdNumber + 1
lSongCounter = 0
End If
Next
' Save the playlist.
Playlist.save("c:\wmpub\wmroot\ModifiedPlaylist.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;
IXMLDOMElement[] Ad_Elements = new IXMLDOMElement[256];
IXMLDOMElement Root_Node;
IXMLDOMElement New_Element;
IXMLDOMNodeList NodeList;
IXMLDOMNode Attr;
IXMLDOMNode Song_Child;
IXMLDOMNode Next_Child;
int iIndex;
long lSongCounter;
long lAdNumber;
try
{
// Create the WMSServer object.
Server = new WMSServerClass();
// Load an existing playlist.
Playlist = Server.CreatePlaylist();
Playlist.load("c:\\wmpub\\wmroot\\Playlist.wsx");
// Find the root node. The root node of a server-side
// playlist must be the smil element.
Root_Node = Playlist.documentElement;
// Retrieve a list of the media elements in the smil
// time container. In this example, all child elements of
// the root node are media elements.
NodeList = Root_Node.getElementsByTagName("media");
// Create a media element for the playlist and identify
// it as an advertisement by setting the role attribute
// to "Advertisement". This enables logging plug-ins to log
// information about the ads.
Ad_Elements[0] = Playlist.createElement("media");
Ad_Elements[0].setAttribute("role", "Advertisement");
Ad_Elements[0].setAttribute("src", "c:\\wmpub\\wmroot\\Ad_1.wmv");
Ad_Elements[0].setAttribute("id", "media_advertisement");
// Initialize variables to count the number of songs and ads.
lSongCounter = 0;
lAdNumber = 1;
// Iterate through the list to find the songs. In this example
// a song is identified by a role attribute equal to "song".
for (iIndex=0; iIndex<NodeList.length; iIndex++)
{
// Retrieve the role attribute for each media element
// and check to see whether it equals "song".
Attr = NodeList[iIndex].attributes.getNamedItem("role");
if (Attr.nodeValue.ToString() == "song")
{
lSongCounter = lSongCounter + 1;
}
// Insert an ad after two songs.
if ((lSongCounter != 0) && (lSongCounter % 2 == 0))
{
// Clone Ad_Elements(0) to add the same advertisement
// each time.
Ad_Elements[lAdNumber] = (IXMLDOMElement)Ad_Elements[0].cloneNode(false);
// Retrieve the media element associated with the role attribute.
Song_Child = NodeList[iIndex];
// Find the next child node.
if (Root_Node.lastChild != Song_Child)
{
Next_Child = Song_Child.nextSibling;
// Insert the ad in front of the next child node. If the song
// is the last media element in the playlist, append the ad.
if (Root_Node.lastChild != Song_Child)
{
New_Element=(IXMLDOMElement)Root_Node.insertBefore(Ad_Elements[lAdNumber], Next_Child);
}
}
else
{
New_Element=(IXMLDOMElement)Root_Node.appendChild(Ad_Elements[lAdNumber]);
}
lAdNumber = lAdNumber++;
lSongCounter = 0;
}
}
// Save the playlist.
Playlist.save("c:\\wmpub\\wmroot\\ModifiedPlaylist.wsx");
}
catch (Exception exc)
{
// TODO: Exception handler goes here.
}
finally
{
// TODO: Clean-up code goes here.
}
This example code creates the following playlist.
<?wsx version="1.0" ?>
<smil>
<media role="song" src="c:\wmpub\wmroot\Song_1.wma"/>
<media role="song" src="c:\wmpub\wmroot\Song_2.wma"/>
<media role="Advertisement" src="c:\wmpub\wmroot\Ad_1.wmv"
id="media_advertisement"/>
<media role="intro" src="c:\wmpub\wmroot\Presentation_Intro.wmv"/>
<media role="presentation" src="http://encoder_name:80"/>
<media role="song" src="c:\wmpub\wmroot\Song_3.wma"/>
<media role="song" src="c:\wmpub\wmroot\Song_4.wma"/>
<media role="Advertisement" src="c:\wmpub\wmroot\Ad_1.wmv"
id="media_advertisement"/>
</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