A Mp3 Streaming Server

MP3 STREAMING SERVER

Here is an application that uses coldfusion server to stream MP3 using an M3U file. An M3U file is basically just a playlist. In principle, if you send a *.m3u file to a media player, it should play the data in the m3u file. M3u file contains data like, location of the MP3.

Step 1!
Locate all your MP3 files and put them in a folder. In my case it was d:\MP3 

Step 2 !
Open you web server (in my case it was IIS). Add the MP3 folder as a virtual directory to the web server. This will ensure that the streaming server will use HTTP port 80 for playing the MP3 on client side.

Step 3 
Create the following file list and follow directions given below !!!

- Application.cfm 
- Default.cfm 
- Login.cfm 
- LoginProcess.cfm 
- Index.cfm 
- DirListing.cfm 
- CreateM3U.cfm 
- Logout.cfm


APPLICATION.CFM

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
    <head>
        <title>
Streaming Media Server</title>
    </head>
    <link rel=
"stylesheet" title="Style" href="Style/StreamStyle.css">
<body>


<cfapplication 
        name=
"Mp3Stream" 
        clientmanagement=
"Yes" 
        sessionmanagement=
"Yes" 
        sessiontimeout=
"#CreateTimeSpan(0,0,20,0)#" 
        applicationtimeout=
"#CreateTimeSpan(0,1,0,0)#">

<cflock  timeout="15"
            throwontimeout="No"
            name="AppLock"
           
type="exclusive">
    
        <cfset AppIsInit = IsDefined(
'Application.Initialized')>

</cflock> 

<cfif AppIsInit eq "No">

    <cflock timeout=
"15"
                throwontimeout="No"
                name="AppLock"
                type="exclusive">

            <cfset AppIsInit =
"Yes">
            <cfset Application.IsInitialized =
"Yes">
            <!---------------------------------------------------------------------
                NOTE: MP3 folder should be a Virtual directory in IIS 
            ---------------------------------------------------------------------->

            <cfset Application.DSN =
"MusicDB">
            <cfset Application.Dir =
"E:/MP3">
            <cfset Application.PlayList=
"http://yourdomain/MP3">
    </cflock>
</cfif>

<!---------------------------------------------------------------------
check if session is initialized
---------------------------------------------------------------------->

<cflock timeout="15"
            throwontimeout=
"No"
            name=
"SessionLock"
            type=
"EXCLUSIVE">
    <cfparam name=
"SessionIsInit" default="#IsDefined('Session.IsInitialized')#">
</cflock>

<!---------------------------------------------------------------------
if session is not initialized, then redirect
---------------------------------------------------------------------->

<cfif SessionIsInit eq 'NO'>
    <cflock timeout=
"15"
                throwontimeout=
"No"
                name=
"SessionLock"
                type=
"EXCLUSIVE">

        <cfif CGI.Script_Name
neq "/CFStream/Default.cfm" and
                CGI.Script_Name
neq "/CFStream/LoginProcess.cfm" and
                CGI.Script_Name neq "/CFStream/Logout.cfm">

            <cflocation url="/CFStream/Default.cfm" addtoken="no">

        </cfif>
    </cflock>

    <!---------------------------------------------------------------------
        is sesion is initialized, then show user menu
    ---------------------------------------------------------------------->

<cfelse>
    <table width="100%">
        <tr>
            <td width=
"50%" align="left">
                <a href=
"Index.cfm">LISTINGS</a>
            </td>
            <td align=
"right">
                <a href=
"Logout.cfm">LOGOUT</a>
            </td>
        </tr>
    </table>

    <hr>
</cfif>
<!---------------------------------------------------------------------
if user navigates to logout, clear all structures
---------------------------------------------------------------------->

<cfif CGI.Script_Name eq "/CFStream/Logout.cfm">
    <cfset SessionIsInit =
"No">
</cfif>


</body>
</html>


DEFAULT.CFM
<!---------------------------------------------------------------------
*This is basically a login form !
*Declare error codes
---------------------------------------------------------------------->

<cfparam name="ErrorCode" default="0">
<cfswitch expression=
"#ErrorCode#">
    <cfcase value=
"0"><cfset Title = "LOGIN TO STREAMING MEDIA SERVER"></cfcase>
    <cfcase value=
"1"><cfset Title = "ERROR WHILE LOGING IN. PLEASE TRY AGAIN"></cfcase>
</cfswitch>

<!--------------------------------------------------------------------------
Login Form
--------------------------------------------------------------------------->


<h3 align="center"><cfoutput>#Title#</cfoutput></h3>
<table align="center" bgcolor="#efefdd">
    <cfform action="LoginProcess.cfm">
    <tr>
        <th>
Username:</th>
        <td>
           
<cfinput name="Username" type="text" required="yes" message="Cannot proceed without a valid login ID">
        </td>
    </tr>
    <tr>
        <th>
Password:</th>
        <td>
           
<cfinput name="Password" type="password" required="yes" message="Cannot proceed without a valid password">
        </td>
    </tr>
    <tr>
        <td colspan=
"2" align="right">
            <input type="submit" name="Submit" value="Login">
            <input type=
"reset" name="Reset" value="Reset">
        </td>
    </tr>

    </cfform>
</table>

LOGINPROCESS.CFM

<cfset CompareUser = Compare(Form.Username, 'Username')>
<cfset ComparePass = Compare(Form.Password,
'Password')>

<cfif CompareUser
eq 0 and ComparePass eq 0>
    <cfset Session.IsInitialized =
"True">
    <cfset SessionIsInit =
"Yes">
    <cflocation url=
"Index.cfm" addtoken="no">
<cfelse>
    <cflocation url=
"Login.cfm?ErrorCode=1" addtoken="no">
</cfif>


INDEX.CFM

<cfdirectory action="LIST" directory="#Application.Dir#" name="MP3Dir">
<cfset DirList =
ValueList(MP3Dir.Name)>
<cfset LoopCount =
0>

<table align="center" width="100%">
    <tr> 
    <cfloop index="SubDirs" list="#DirList#">
        <cfset LoopCount = LoopCount
+1>
        <td align="right"><cfoutput>#LoopCount#</cfoutput></td>
        <td bgcolor=
"<cfoutput>#iif(LoopCount mod 2, DE('efeddd'), DE('White'))#</cfoutput>">
            <a href=
"DirListing.cfm?Dir=<cfoutput>#URLEncodedFormat('#SubDirs#')#</cfoutput>">
           
<cfoutput>#SubDirs#</cfoutput>
            </a>
        </td>
    <cfif LoopCount Mod 3 eq 0>
    </tr>
    </cfif>
    </cfloop>

</table>

DIRLISTING.CFM


<cfdirectory action="LIST"
                directory="#Application.Dir#\#URL.Dir#"
                name=
"SongList"
                filter=
"*.mp3">
<cfform action=
"CreateM3U.cfm">
<h3 align="center"><cfoutput>#URL.Dir#</cfoutput></h3>
<table width="60%" align="center">
    <tr bgcolor=
"#ECD9BB">
        <th></th>
        <th>
Title</th>
        <th>
Size</th>
    </tr>
    <cfoutput query="SongList">
    <tr bgcolor="#iif(currentrow mod 2, DE('efefdd'), DE('White'))#">
        <td>
<cfinput type="Checkbox" name="SongSelect" value="#URL.Dir#/#Name#"></td>
        <td>
#Name#</td>
        <td>
          
  <cfset SizeInKB = NumberFormat(size/1000000, '__.__')>
           
#SizeInKB# Mb
        </td>
    </tr>

    </cfoutput>
    <tr>
        <td colspan=
"3"><hr></td>
    </tr>

    <tr bgcolor="#ECD9BB">
        <td colspan="3" align="right">
            <input type="submit" name="Play" value="Play Song List">
        </td>
    </tr>
</table>

</cfform>


CREATEM3U.CFM
<cfset SelectedList="#Form.SongSelect#">

<cfloop index=
"PlayListLoop" list="#SelectedList#">
    <cffile action=
"APPEND"
            file=
"#Application.Dir#/PlayList.m3u"
            output=
"#Application.PlayList#/#PlayListLoop#"
            addnewline=
"Yes">
</cfloop>


<cfheader NAME=
"Content-disposition" VALUE="inline;filename=PlayList.m3u">
<cfcontent type=
"text/plain" file="#Application.Dir#/PlayList.m3u" deletefile="Yes">

LOGOUT.CFM

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta http-equiv=
"refresh" content="2;url=default.cfm">
<html>
    <head>
    </head>
    <body>
        <h3 align="center" style="position:absolute; top:50%; left:35%;">Logged out successfully !!!</h3>
        <cfset StructClear(Session)>
        <cfset StructClear(Application)>

    </body>
</html>



THERE YOU ARE DONE !!!
You can also use some sort of a mechanism to control the number of simultaneous users.

All ColdFusion Tutorials By Author: Anang A Phatak
  • Basic Calculator
    This is a tutorial to build a simple calculator for your web pages. You should get it pretty easily as you browse through the code.
    Author: Anang A Phatak
    Views: 20,206
    Posted Date: Tuesday, October 14, 2003
  • An E - Rolodex System
    Quick and easy way to sort and list your contacts by last names, in two really really simple steps. Almost everybody who uses a data-driven website has a database containing some information about the users. I had a database table called "contact" in which user's first name, last name, address, zip, email, phone, fax.... everything was listed. If you do have something like that and wish to draw and group your contacts by say last names, maybe you will find this tutorial useful.
    Author: Anang A Phatak
    Views: 19,257
    Posted Date: Friday, November 7, 2003
  • A plot to plot a line
    I had no work one day due to a worm attack on our servers, thus a plot to plot a line on a graph was hatched in my empty mind. These files show you, how to plot a line using no database, no java, no long wait times for aplet loading, just 3 tools, Loop, table and text.
    Author: Anang A Phatak
    Views: 17,387
    Posted Date: Thursday, May 6, 2004
  • Automatic Form Generator
    This is not a tutorial as such, more like an application that you can put in a directory. It could boring if you use the CF editor, to pick "cfform" fill its attributes, then pick "cfinputs" one by one. fill out those attributes... one by one, then change tabs and pick the "submit" button... so on and so forth. Even if you code in a note pad, it might get lengthy to code individual element. Wouldn't it be nicer to code all these elements at once, then just copy the code and paste it in your editor?
    Author: Anang A Phatak
    Views: 20,506
    Posted Date: Thursday, May 20, 2004
  • A random password generator
    RANDOM PASSWORD GENERATOR SCRIPT ! I know there is a random password tutorial here already. This is just another way to do the same. I think this is a little easier to understand. Refresh it to generate a new password string everytime !
    Author: Anang A Phatak
    Views: 22,824
    Posted Date: Monday, May 24, 2004
  • Advanced Calculator
    I have posted a "Basic Calculator" tutorial here. That was more like a representation of how you would calculate with a paper and a pencil. You provide INPUT A then a MATHEMATICAL OPERATION like a "+" or a "-" and then an INPUT B. This is more a represntation of how you would use a regular hand-held calculator complete with buttons for NUMBERS, OPERATIONS and CLEAR TEXT.
    Author: Anang A Phatak
    Views: 17,869
    Posted Date: Friday, June 18, 2004
  • Automatically Query To CFM
    This is a custom tag application. The cf_QueryRender custom tag takes your query arguments and gives you a final page table and all...
    Author: Anang A Phatak
    Views: 24,169
    Posted Date: Friday, October 29, 2004
  • A Mp3 Streaming Server
    This is a small application that shows you how to create an MP3 streaming server.
    Author: Anang A Phatak
    Views: 25,181
    Posted Date: Monday, November 8, 2004
  • Breaking down your query results into pages (Paging Tutorial)
    I havent come across a "paging" tutorial on this site. I know there are JavaScripts available that help you achieve this, and the DataSet object in VB.Net comes with paging. All you do is "enable paging". But how do you do it in ColdFusion ?
    Author: Anang A Phatak
    Views: 29,600
    Posted Date: Tuesday, November 16, 2004
  • A DataSet just like VB.Net
    This tutorial shows you how to create a "dataset" just like the one in VB.Net In VB.Net you would create a dataset with "edit" button in an extra column. Once you click "edit", you get an option to "update", "delete" or "cancel edit mode" This is just like a cfgrid tag. Although a cfgrid tag lets you bulk insert, bulk update or bulk delete, the dataset object does it one by one. But cfgrid is slower, and may give users Java errors, depending on their browser settings.
    Author: Anang A Phatak
    Views: 32,273
    Posted Date: Wednesday, November 17, 2004
  • Dynamic time and date for your pages
    Have you seen the "www.EasyCFM" page closely? On the main page, top right, there is a place for time, and top left a place for day-date. Ever wonder how Pablo does it ? This is not a ColdFusion tutorial. Its JavaScript.
    Author: Anang A Phatak
    Views: 20,249
    Posted Date: Wednesday, January 5, 2005
  • ColdFusion MX 6.1 Installation on Linux (Ubuntu -- Hoary Hedgehog)
    I have tried hoards of websites on how to install coldfusion on Fedora Core 3 with apache webserver. For some reason the connectors always failed. I had "Ubuntu" on my laptop, basically because "acpi" suspend/hibernate actually works. I decided I might try to install CF there to find out what was going wrong. Surprisingly everything worked like a charm. Make sure you use "apt-get install apache2" before you try this. BEST OF LUCK ....
    Author: Anang A Phatak
    Views: 22,649
    Posted Date: Tuesday, May 10, 2005
  • Dynamic textbox and progress bar for your pages
    The principle of this tutorial is similar to "Dynamic time and date for your pages" tutorial. Except that this one generates messages, and that one updated time. Read on, you will get the hang of it....
    Author: Anang A Phatak
    Views: 24,840
    Posted Date: Thursday, May 19, 2005
  • Getting ColdFusion Studio for Linux
    I like using HomeSite+ for windows, and I am getting used to Dreamweaver Mx. But I still need something just as good for Linux. For some reason, I couldnt get "wine and Dreamweaver Mx" to work. So I "Googled" a bit and stumbled upon Eclipse and cfEclipse. Here is how to set it up.
    Author: Anang A Phatak
    Views: 22,885
    Posted Date: Wednesday, May 25, 2005
  • Breaking down your query results into pages (Paging Tutorial) Part-II
    This is an extension to my last tutorial "Breaking down your query results into pages (Paging Tutorial)" which is posted here on www.easycfm.com In the last tutorial, you could retrieve a dataset with a , then use a technique to seperate the results over several pages. It simply ; - took the total "recordCount" - divided that with the "number of records per page" - then displayed the number of pages at the bottom of the table. This is a little more sophisticated than that. Read on...
    Author: Anang A Phatak
    Views: 18,106
    Posted Date: Wednesday, January 11, 2006
Download the EasyCFM.COM Browser Toolbar!