Asp.Net音乐播放器

时间:2013-06-14 16:00:06

标签: c# asp.net error-handling mp3 audio-player

我正在按照指南创建一个简单的音乐目录。

它从文件夹中读取并将音乐名称放在网格上,然后在网格上添加一个应该将文件发送给播放器的“播放按钮”。

这是背后的代码:

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;

namespace WebApplication8
{
    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("your music directory here");
            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.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("your music directory here");
            mySrc = di + grid.SelectedDataKey.Values["SongName"].ToString();

        }
    }
}

这是头版代码:

 %@ Page Language="C#" AutoEventWireup="true" CodeBehind="MusicPlayer.aspx.cs"     Inherits="WebApplication8.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 id="Head1" 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="0" />
                                <!--[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="0" />
                                </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>

我遇到的问题是:我可以在页面上加载内容然后显示,但是当我点击gridview上的播放按钮时,它给了我一个恐怖:

mySrc = di + grid.SelectedDataKey.Values["SongName"].ToString();
NullReferenceException was unhandled by user code.

我可以看到mySrc返回一个空值,但是当我在di和grid.selected中“看”时,那里有值。

有人可以帮助我并告诉我,我做错了什么?

提前感谢并抱歉我的英语不好,而不是我的母语。

编辑:我发现在我的首页代码中,KeyDataName被设置为“songname”,我将其更改为SongName以匹配后面的代码,现在它不会给我一个错误不再,但只是不做任何事情。我点击播放,没有任何反应。

再次感谢。

0 个答案:

没有答案