Thursday, 22 November 2012

Music player: Embed with ASP.NET C# Web Page

MusicPlayer.aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MusicPlayer.aspx.cs" Inherits="MusicPlayer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        var oldgridSelectedColor;
        function setMouseOverColor(element) {
            oldgridSelectedColor = element.style.backgroundColor;
            element.style.backgroundColor = '#dcdcdc';
            element.style.cursor = 'hand';
        }
        function setMouseOutColor(element) {
            element.style.backgroundColor = oldgridSelectedColor;
            element.style.textDecoration = 'none';
        }
    </script>
    <style type="text/css">
     
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
 
    <table>
        <tr>
            <td>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server"
                    UpdateMode="Conditional" ChildrenAsTriggers="False">
                    <Triggers>
                        <asp:AsyncPostBackTrigger
                            ControlID="grid"
                            EventName="Load">
                        </asp:AsyncPostBackTrigger>
                    </Triggers>
                    <ContentTemplate>
                        <object classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" id="player"  class="musicplayer-media">
                        <param name="url" value= "<% =mySrc %>" />
                        <param name="src" value="<% =mySrc %>" />
                        <param name="name" value="musicplayer" />
                        <param name="showcontrols" value="true" />
                        <param name="animationatStart" value="true" />
                        <param name="volume" value="" />
                        <!--[if !IE]>-->
                        <object type="video/x-ms-wmv" data="<% =mySrc %>" class="musicplayer-media">
                            <param name="src" value="<% =mySrc %>" />
                            <param name="name" value="musicplayer" />
                            <param name="autostart" value="true" />
                            <param name="controller" value="true" />
                            <param name="volume" value="" />
                        </object>
                        <!--<![endif]-->
                        </object>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
        <tr>
            <td>
                <asp:GridView ID="grid" runat="server" BackColor="#CC9900" ForeColor="#663300"
                    onrowdatabound="grid_RowDataBound"
                    onselectedindexchanged="grid_SelectedIndexChanged" DataKeyNames="songname">
                    <Columns>
                        <asp:ButtonField CommandName="Select" Text="Play" />
                    </Columns>
                    <SelectedRowStyle BackColor="#66CCFF" BorderColor="Black" BorderStyle="Solid" />
                </asp:GridView>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Media Player Volume Control in - ASP.NET using C#:
The code <param name="volume" value="100" /> should set the media player volume to Full but it does not. Then I tried to put the value="0" and it actually works.
  • So correct code is: <param name="volume" value="0" />

MusicPlayer.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Text;

public partial class MusicPlayer : System.Web.UI.Page
{
    public string mySrc;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getSongName();
        }
    }
    public void getSongName()
    {
        DirectoryInfo di = new DirectoryInfo("C:/Inetpub/wwwroot/MasudAhmed/Songs/");
        if (di.Exists == true)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("SongName");
            foreach (FileInfo fi in di.GetFiles("*.mp3", SearchOption.AllDirectories))//get filename from this directory
            {
                DataRow dr = dt.NewRow();
                dr["SongName"] = fi.Name;//show only song name excluding path
                dt.Rows.Add(dr);
            }
            //grid.Columns[1].HeaderText = "Here are my favourite songs:";
            grid.DataSource = dt;
            grid.DataBind();
        }
    }

    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
        e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
    }
    protected void grid_SelectedIndexChanged(object sender, EventArgs e)
    {
        DirectoryInfo di = new DirectoryInfo("C:/Inetpub/wwwroot/MasudAhmed/Songs/");
        mySrc = di + grid.SelectedDataKey.Values["songname"].ToString();

    }
}

No comments:

Post a Comment