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.

About This Tutorial
Author: Anang A Phatak
Skill Level: Intermediate 
 
 
 
Platforms Tested: CF4,CF5,CFMX
Total Views: 111,028
Submission Date: November 08, 2004
Last Update Date: June 05, 2009
All Tutorials By This Autor: 12
Discuss This Tutorial
  • In the step 3, im stuck with it.well i hv created those files names using notepad but property i set .CFM eg. LOGOUT.CFM as all type of file. and hv saved all these files under D:\MP3 as well.I hv used IIS to create virtual directory to D:|MP3 as wel.. Now wil someone teach me how to test whether i hv successfully done with streaming mp3 parts.. I tried opened Internet explorer and typed htpp://localhost issit? or type what?htpp://localhost/index.cfm???

  • I have added your site in the bookmarks as for a long time did not meet anything similar! Thanks.

  • This is excellent, I am having problems, I can do everything now (with a bit of playing) I can get it to list all of my MP3s, and when I go to click on one it goes the the fourm http://localhost:8500/mp3%20stream/DirListing.cfm and I click on Play Song And it crashes from there Can someone help me get this up and running please! Thanks Jamie - Please E-mail!

  • what does the database look like that goes with this application?

  • Do you know what a Video Play List looks like? If you do, you may be able to use it like I have used the "m3u" file. I am not sure about this. (Try googling for SMIL tutorials) The best way to stream videos would be to install Win 2003 server and start Media Services. That’s the easiest thing to do. Otherwise get one of the free media servers like Helix (try to Google for “Free Media Server”, there are several of them), and run them on Win XP or Linux. You probably will not get a great performance by using CF to stream videos anyway. Hope you find this useful Anang

  • Can you use the same technic to Stream Video with CF?

  • All I get is a page cannot be displayed error.

  • "Goliath" brought to my attention the missing login.cfm file. Please change this line in LoginProcess.cfm as given below; Instead of use; Hope this helps, Anang

Advertisement

Sponsored By...
Deep Tissue Massage and Swedish Massage Services just $39 for a 50 minute massage!