Click here to Skip to main content
6,932,415 members and growing! (19,416 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Advanced License: The Code Project Open License (CPOL)

Create A Music Playlist for Windows Media Player At Your Webpage

By Mohd Arshad (Sam)

This article will help you to create a music playlist at your webpage and change the running song accordingly on embedded media player. + Embedding a Windows Media Player using JavaScript.
C# (C#2.0), Javascript, HTML, Windows (Win2K, WinXP, Win2003, Vista), .NET (.NET2.0), ASP.NET, Visual-Studio (VS2005), ADO.NET, WebForms, Architect, Dev
Posted:22 Nov 2008
Views:12,952
Bookmarked:12 times
Unedited contribution
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 1.12 Rating: 1.60 out of 5
3 votes, 60.0%
1
1 vote, 20.0%
2

3

4
1 vote, 20.0%
5

Download Playlist_Source.zip - 1.83 KB

Introduction

This article will help you to create a music playlist at your webpage and change the running song accordingly on embedded media player. + Embedding a Windows Media Player using JavaScript. You'll embed windows media player with a new filepath fetched from database (using cookies), each time when a new song title has been clicked at playlist.

First Create Some Database Stuff

Create a table in master database (or any other database, relevent to connection string you are using) like this…

create table medPL(sid int identity(1,1), sname varchar(20), surl varchar(100))
Now insert song titles and urls like this…
insert into medPL (sname,surl) values ('Mr. Lonely','c:\lonely.mpg')
Make sure that you provided correct path and file name and type.

Now You Can Try By Using Downloads

Download .ZIP file, extract two pages (Default.aspx, Default.aspx.cs). Create a new website in VS using Visual C# at codebehind. Delete the existing Default.aspx page from your site. Now, right click the project and add existing item – which are your downloaded pages. Run and enjoy...

How The Process Go On

  • You will create a GridView, where your Playlist will be shown using DataTextField sname in your table. (e.g. Mr. Lonely).
  • This Playlist will be of type Hyperlink.
  • Everytime you click an item, it will navigate to same page but with a querystring. This querystring will have value of song id.
  • Using another SqlDataSource tag you’ll get the song url for that id.
  • On Page Load, you will create a cookie and give that the value as song url.
  • Now, You will embed your media player using JavaScript code and after fetching the cookie value, you’ll provide that to filename parameter of object tag.

This process will be repeated each time you will click a new song.

Code Blocks For .ASPX Page

Creating your playlist, using Grid HyperLinkField

<div>
     <asp:GridView ID="grdMPL" AutoGenerateColumns="false" DataSourceID="dsMedPL" runat="server">
      <Columns>
         <asp:HyperLinkField
              HeaderText="Playlist"
              DataTextField="sname" 
              DataNavigateUrlFields="sid"
              DataNavigateUrlFormatString="Default.aspx?sid={0}"/>                     
      </Columns>
   </asp:GridView>
   <asp:SqlDataSource ID="dsMedPL" ConnectionString="server=.;database=master;uid=sa;pwd=sa" 
              SelectCommand="select sid,sname from medPL"
              runat="server">        
   </asp:SqlDataSource>
</div>

Reserving the Place for Media Player using html division
<div id="divPlayer"></div>

Fetch the Song URL, that is been selected from playlist. Using QueryString Value. Now see Page_Load function at codebehind
<asp:SqlDataSource 
     id="dsSelURL" 
     ConnectionString="server=.;database=master;uid=sa;pwd=sa"
     SelectCommand="select surl from medPL where sid=@sid"
     Runat="server">
     <SelectParameters>
          <asp:QueryStringParameter Name="sid" QueryStringField="sid" />
     </SelectParameters>
</asp:SqlDataSource>

Request Cookie Value, Embed Media Player using JavaScript Code
    <script type="text/javascript">
        function getCookieValue()
        {
           var cookieValue = document.cookie;
           var cookieStartsAt = cookieValue.indexOf(" " + "songURL" + "=");
           if (cookieStartsAt == -1)
           {
              cookieStartsAt = cookieValue.indexOf("songURL" + "=");
           }
           if (cookieStartsAt == -1)
           {
              cookieValue = null;
           }
           else
           {
              cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
              var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
              if (cookieEndsAt == -1)
              {
                 cookieEndsAt = cookieValue.length;
              }
              cookieValue = unescape(cookieValue.substring(cookieStartsAt,
                 cookieEndsAt));
           }
           return cookieValue;
        }
        document.getElementById('divPlayer').innerHTML='<object id="mediaPlayer" height="300" width="300" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" visible="true">+ getCookieValue() + '"/></object>';
    </script>

Code Block For .ASPX.CS Page

    protected void Page_Load(object sender, EventArgs e)
    {
        //Accessing the selected song URL
        GridView grd = new GridView();
        grd.DataSource = dsSelURL;
        grd.DataBind();

        //Creating a Cookie by giving this the value of Selected Song URL.
        if (grd.Rows.Count == 1)
        {
            Response.Cookies["songURL"].Value = grd.Rows[0].Cells[0].Text;
        }
    }

Final Words

Now your Music Playlist Project is complete. Run and Enjoy...
I hope you will find this article helpful in programming for Music Playlist, Embedding Windows Media Player using Javascript, Using QueryString, Using Cookies. For more information you can contact me at arshad@m-arshad.co.cc Good Luck!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohd Arshad (Sam)


Member

Software professional with demonstrated strength in windows-based and web-based software development. Have 4 years of experience with the full software development lifecycle including requirements, design, development, testing/QA, deployment, training & support. Have 1 year experience managing groups, planning and executing implementations. Practical working knowledge of all aspects of IT. Possess strong insight into practical business considerations.


www.cherisys.com
www.webstreet.in
www.mohdarshad.in


Occupation: Software Developer (Senior)
Company: Cherisys Technologies, WebStreet.in
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralCode Change For Firefox Support Pinmembercovenof1311:59 15 Jan '09  
GeneralMy vote of 1 Pinmemberwegenerb13:28 23 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 22 Nov 2008
Editor:
Copyright 2008 by Mohd Arshad (Sam)
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project