PaulSadowski.com 

PaulSadowski.com
ASP Playlist
The Active Server Page illustrated below creates a playlist to be sent to Windows Media Player. The playlist consists of twenty songs chosen randomly from a database of available files.

This sample uses WMA files (Windows Media Audio) but you can adapt it to include any other file type you like. The function Title which creates a Title element for an item is the only piece of code that would have to be modified for additional file types.

What happens here is the ASP page makes a database connection to your db with the file information. It selects 20 files randomly and sends them back to Windows Media Player in an ASX playlist format. Windows Media Player then does the real work playing each song in order.

If you don't have access to Windows Media Server you can change the code to use HTTP protocol rather than the MMS protocol to play the files. Keep in mind that by using HTTP there is no file protection. Anyone can save to their own disk the music files. The MMS stream does not normally allow that.

No matter which protocol you use you must create a virtual directory that contains your music files. Your database should consist of a table with at least one field named files. You could if you like use different tables in the database for different types of music such as folk, oldies, rock, jazz, etc.

In this sample we include the full server path to the file in the file field. That allows us to use the same code for any table of music.

One method of creating the list would be to use a batch file like this one with the output redirected to a file.

for /f "tokens=*" %%c in ('dir /b /-s *.wma') do (
  echo /virtualroot/%%c
)

You then import the data in the file into Access or first into Excel and then into Access as you prefer.

This is our sample database named WebMusic.mdb. It has a table called Oldies which is illustrated here.

ID File
1 /oldies/70_080 - Kinks - Lola.wma
2 /oldies/71_064 - Chicago - Does Anybody Really Know What Time It Is.wma

The sample code requires changes to the server name, the path to the database and the correct table name within the database. Otherwise it should function as-is. The server name can be either an IP, a fully qualified domain name as illustrated or a WINS name such as BLISS if the server were named BLISS.

You can add as many modification as you like. For example you may say the first item should be a welcome message. Perhaps the 10th item would a message reminding the listeners what server they are listening to ("Welcome to the Bliss broadcast server."). And the last item may be a thank you message. It's all up to you.

<%
Dim
URLs()
Response
.ContentType = "video/x-ms-asf"

Prefix = "HREF=" & CHR(34) & "mms://mediaserver.your.com"
Suffix = CHR(34) & " />"

Set
OBJdbConnection = CreateObject("ADODB.Connection")
OBJdbConnection.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\ASFRoot\WebMusic.mdb"

SQLQuery = "SELECT file FROM Oldies"

Set
Result = OBJdbConnection.Execute(SQLQuery)
IDX = 0
 
if
Not Result.EOF Then
  Do
While Not Result.EOF
    Redim
Preserve URLs(IDX)
    URLs(IDX) = Prefix & Result("file") & Suffix
    IDX = IDX + 1
    Result.MoveNext
  loop
end
if

head = "<ASX Version = ""3.0"">" & vbCRLF & "<TITLE>Broadcast</TITLE>" & vbCRLF
foot = vbCRLF & "</ASX>"
y = head

for
x = 0 to 19
  ThisRnd = RandomNumb(UBound(URLs), 0)
  ThisTitle = URLs(ThisRnd)
  s = "<ENTRY>" & vbCRLF
  s = s & " <TITLE>" & BaseName(Title(ThisTitle)) & "</TITLE>" & vbCRLF
  s = s & " " & URLs(ThisRnd) & vbCRLF
  s = s & "</ENTRY>
" & vbCRLF
  y = y & s
next

y = y & foot Response.Write y & vbCRLF

'Return the filename portion of a full pathname
Function
Basename(FullPath)
dim
x, y, tmpstring

tmpstring = FullPath
x = Len(FullPath)
for
y = x to 1 step -1
  if
mid(FullPath, y, 1) = "\" or _ mid(FullPath, y, 1) = ":" or _ mid(FullPath, y, 1) = "/" then
 
  tmpstring = mid(Fullpath, y+1)
    exit
for
  end
if
next

Basename = tmpstring
end
function

'Returns a random number between HI and LO, inclusive
Function
RandomNumb(hi, lo)
Randomize

RandomNumb = Int((hi - lo + 1) * Rnd + lo)
end
function

function Title(strTitle)
strTitle = Replace(strTitle,"HREF=" & CHR(34) & "mms://mediaserver.your.com", "")
strTitle = Replace(strTitle, ".wma" & CHR(34) & " />", "")
strTitle = Replace(strTitle, ".", " ")
strTitle = Replace(strTitle, "-", " ")
Title = strTitle end function

%>

Remarks

Response.ContentType is required to inform the browser of the proper MIME type for the playlist. This will cause the browser to open Windows Media Player (or the default program for .asx files) rather than displaying the list in the browser window.

This is particularly important in Windows XP SP2 which verifies the MIME typing on all downloaded files.

 

© 2005 by Paul R. Sadowski 
All Rights Reserved. Used By Permission.
Comments to: scripting@paulsadowski.com