2005-03-02
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 2 of 2 )
Creating the Site
Now we can create our site. My goal was to produce a single page that could give me a tree list of all of my music by album; there is so much that I am not interested in picking individual songs. The user interface will look like figure 7 when we're done.
I am not going to go step-by-step of how I created this site, but focus on the code that is specific to Windows Media Services and playlists. The idea is to pick artists or albums to include to a playlist and click "Add to Playlist" to create a playlist of songs on the right side. Once the playlist is created, the user can click either "Playlist (mms)" or "Playlist (http)" to get a playlist of either mms or http (with my 8080 port) saved to his browser, so he can open it in Windows Media Player.
In my example, I use Infragistics' UltraWebTree control for the tree control; you can replace it with any Web tree control.
The core of the code is still simple enough. I go through the directory with all my MP3s and re-create the directory structure in the tree without the individual files. Once the user has checked all the nodes he wants, I add the songs to a simple array and bind it to a list of songs on the right side of the page, as seen in figure 8:
To send each of the songs individually (so the player knows what songs it is playing), we have to put together a playlist. The playlist format here is the ASX file.
The ASX file looks like so:
<ASX VERSION="3.0" PREVIEWMODE="NO" BANNERBAR="auto" >
<ENTRY >
<TITLE>Sample Song</TITLE>
<REF HREF="http://sample.microsoft.com/sample1.mp3" />
</ENTRY>
</ASX>
The format of the file is simple. For every song in the playlist, you need an ENTRY section. The entry has a title and ref section to name and point to the media.
While this looks innocently like XML, it is not. The elements are not case sensitive, and the parser in Windows Media Player choke if you include a processing header (e.g. <? Xml version="1.0">). But I hate munging text, so I go ahead and use an XmlDocument to create the playlist:
MemoryStream CreateASX(SortedList songs, string server)
{
const string PUBPOINT = "OurMusic";
XmlDocument asx = new XmlDocument();
XmlElement element = asx.CreateElement("ASX");
element.Attributes.Append(asx.CreateAttribute("VERSION"));
element.Attributes["VERSION"].Value = "3.0";
element.Attributes.Append(asx.CreateAttribute("PREVIEWMODE"));
element.Attributes["PREVIEWMODE"].Value = "NO";
element.Attributes.Append(asx.CreateAttribute("BANNERBAR"));
element.Attributes["BANNERBAR"].Value = "auto";
XmlElement title = asx.CreateElement("TITLE");
title.InnerText = "Shawn Wildermuth's Remote Media Player";
element.AppendChild(title);
foreach (DictionaryEntry entry in songs)
{
FileInfo file = entry.Value as FileInfo;
if (file != null)
{
XmlElement media = asx.CreateElement("ENTRY");
XmlElement href = asx.CreateElement("REF");
href.Attributes.Append(asx.CreateAttribute("HREF"));
href.Attributes["HREF"].Value =
string.Format("{0}{1}{2}",
server,
PUBPOINT,
file.FullName.Substring(ROOTDIR.Length).Replace("\\", "/"));
XmlElement mtitle = asx.CreateElement("TITLE");
mtitle.InnerText = file.Name;
media.AppendChild(mtitle);
media.AppendChild(href);
element.AppendChild(media);
}
}
asx.AppendChild(element);
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(asx.OuterXml);
writer.Flush();
return stream;
}
This code is pretty straightforward. If the user has songs in the song list (the files object), we clear the response and send down a playlist file. The CreateASX method puts together the playlist file, so we can send it down to the client.
I had some trouble getting it to return the file as anything other than default.aspx as a file name. I had to play around with this code for quite a while to allow me to open it as an .asx file (the playlist format we're using). The key seems to be to add the "Content-Disposition" header to allow you to specify a filename.
Now that this all works, I can click on the Playlist button and open the .asx file (which launches Windows Media Player), as seen in figure 9:
I have only scratched the surface of using Windows Media Services. For larger media-based business, Windows Media Services supports subscriptions, banner ads, and more. It is a mature platform for delivering content. Since it is an included part of Windows Server 2003, don't forget to include it in your next project!
Download the source code for this article
![]() |
|


