Thursday, April 16, 2009

Windows Media Server and C#

Hi ALL,
In this post I will introduce you about Windows Media Services, Playlists and
programming WMS. You can use this to create your own internet radio, TV or to
publish Live content like events at your place on your website.

Windows Media Services
Windows Media Services are used to Publish your media content on internet. Where
users can play streams on Players like Windows Media Player, Real Player etc.

I will not explain much about installing/Configuring Windows Media Services. You
can refer to link
http://technet.microsoft.com/en-us/library/cc778708.aspx
http://technet.microsoft.com/en-us/library/cc772032.aspx

PlayLists
Sample Playlist

< ?wsx version='1.0'? >
< smil >
< media src="legacy_content_clip.wmv"/ >
< /smil >

So here it comes more about programming playlist. There are two ways to edit
playlist offline and while content it is streaming or publishing point is
running.

To Add Media to publishing point :

try
{
//Create object of server i.e. Windows Media Services.
WMSServer server = new WMSServerClass();
//Create playlist object
IWMSPlaylist Playlist;
// Create object for Element for Media
IXMLDOMElement element_media;
IXMLDOMNode root, node;

// Loop through All Publish Point
foreach (IWMSPublishingPoint pB in server.PublishingPoints)
{
// Check name of your publishing Point
if (pB.Name == "itradio")
{
//Create IWMSBroadcastPublishingPoint object for your Publishing point
IWMSBroadcastPublishingPoint pp = (IWMSBroadcastPublishingPoint)pB;
//Assign your playlist to Playlist object
Playlist = pp.SharedPlaylist;
//Add Media element to playlist
element_media = Playlist.createElement("media");
//Set Various Attribute e.g. src to give path of your media file or stream from Windows Media Encoder
element_media.setAttribute("src", filePath);
element_media.setAttribute("id", ID);
element_media.setAttribute("begin", "wallclock(" + PlayDateTime + ")");
element_media.setAttribute("duration", "+" + duration + "min");
root = pp.SharedPlaylist.getElementsByTagName("seq")[0];
node = root.appendChild(element_media);
//Save Playlist
Playlist.save("c:\\wmpub\\wmroot\\itradio_playlist.wsx");
}
}
}
catch (Exception ex)
{
}

Similarly you can remove media from playlist by looping through media in
playlist and matching it with attributes and use removechild from playlist:
IXMLDOMNodeList medialist = pp.SharedPlaylist.getElementsByTagName("media");
root = pp.SharedPlaylist.getElementsByTagName("seq")[0];
foreach (IXMLDOMNode media in medialist)
{
for (int iAt = 0; iAt < media.attributes.length; iAt++)
{
IXMLDOMAttribute att = (IXMLDOMAttribute)media.attributes[iAt];
if (att.name.ToString().ToUpper() == "ID")
{
if (att.value.ToString() == ID)
{
node = root.removeChild(media);
}
}
}
}

This way you can programmatically manage you playlist to add and remove media.
You can alter this code to add advertisements and any thing related to list. If
you have knowledge of XML DOM using C# it will be bonus for you. (Some time
later I will post article on XML using C# :) )

Hope you enjoy creating some cool internet radio or TV radio streams. In today’s
world where you want to learn things online this can be great idea to deliver
trainings online from live feeds.

Cheer :)
Upendra Kolte
http://www.ingenioustech.co.in/