我应该如何按照ASP.NET C#中的gridview行选择播放mp3文件 [英] How should I play the mp3 file as per gridview row selection in ASP.NET C#

查看:72
本文介绍了我应该如何按照ASP.NET C#中的gridview行选择播放mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码plz帮我解决这个问题



我尝试了什么:



 GridViewRow gvRow =(GridViewRow)(发送者为Control).Parent.Parent; 
int index = gvRow.RowIndex;
int TID = Convert.ToInt32(gvTrainStatus.DataKeys [index] [TID]);
Session [TID] = TID;

// if(TID = Session [TID])
// {
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(CS);
con.Open();
SqlCommand cmd = new SqlCommand(SPShowData,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ TID,Session [TID]);
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
sqlDa.Fill(dt);
if(dt.Rows.Count> 0)
{



// SoundPlayer sd = new SoundPlayer();
//lblTrainNo.Text = dt.Rows [0] [TrainNo]。ToString();


SoundPlayer player = new SoundPlayer();
player.SoundLocation = AppDomain.CurrentDomain.BaseDirectory +Sound / 0.mp3;
player.Play();

解决方案

您正在尝试使用播放MP3文件SoundPlayer class:

 SoundPlayer player =  new  SoundPlayer(); 
player.SoundLocation = AppDomain.CurrentDomain.BaseDirectory + Sound / 0.mp3;

SoundPlayer类(系统) .Media) [ ^ ]只能播放WAV文件:

引用:

控制播放来自.wav文件的声音。



使用媒体播放器和/或在网上搜索类似c#play mp3的内容,其中提供了大量示例。


您已将此问题标记为ASP.NET。这意味着您的代码在服务器上运行。音频文件将在服务器上播放,其中没有人 - 除了可能是一些困惑的系统管理员 - 都会听到它。



它可能在Visual Studio中调试代码时出现。但这只是因为,在特定情况下,服务器和客户端是同一台计算机。



一旦部署到真实服务器,您的用户就会抱怨声音没有播放。 (你的系统管理员会抱怨你服务器发出的嘈杂声。)



在客户端播放声音 ,您需要使用HTML < audio> 元素以及一些Javascript。

< audio>:嵌入音频元素 - HTML | MDN [ ^

below is my code plz help me to solve that problem

What I have tried:

GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
        int index = gvRow.RowIndex;
        int TID = Convert.ToInt32(gvTrainStatus.DataKeys[index]["TID"]);
        Session["TID"] = TID;

        //if (TID = Session["TID"])
        //{
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection(CS);
            con.Open();
            SqlCommand cmd = new SqlCommand("SPShowData", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@TID", Session["TID"]);
            SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
            sqlDa.Fill(dt);
            if (dt.Rows.Count > 0)
            {



            //SoundPlayer sd = new SoundPlayer();
            //lblTrainNo.Text = dt.Rows[0]["TrainNo"].ToString();
      

            SoundPlayer player = new SoundPlayer();
            player.SoundLocation = AppDomain.CurrentDomain.BaseDirectory + "Sound/0.mp3";
            player.Play();

解决方案

You are trying to play an MP3 file using the SoundPlayer class:

SoundPlayer player = new SoundPlayer();
player.SoundLocation = AppDomain.CurrentDomain.BaseDirectory + "Sound/0.mp3";

But the SoundPlayer Class (System.Media)[^] can only play WAV files:

Quote:

Controls playback of a sound from a .wav file.


Use the Media Player instead and / or search the web for something like "c# play mp3" which provides plenty of examples.


You have flagged this question as ASP.NET. That means your code is running on the server. The audio file will play on the server, where nobody - except maybe some bemused sysadmins - will ever hear it.

It might appear to work when you debug your code in Visual Studio. But that's only because, in that specific scenario, the server and client are the same computer.

As soon as you deploy to a real server, your users will complain that the sound doesn't play. (And your sysadmins will complain about the cacophony emanating from your server.)

To play sound on the client, you need to use the HTML <audio> element, along with some Javascript.
<audio>: The Embed Audio element - HTML | MDN[^]


这篇关于我应该如何按照ASP.NET C#中的gridview行选择播放mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆