Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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();
Posted
Updated 8-Jun-18 6:32am

You are trying to play an MP3 file using the SoundPlayer class:
C#
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.
 
Share this answer
 
Comments
Shahbaz435 8-Jun-18 7:00am    
I had tried but not getting anything
Jochen Arndt 8-Jun-18 7:15am    
What have you tried?

There are multiple solutions to play MP3 files. However, each requires a specific player to be installed that supports MP3.
Shahbaz435 8-Jun-18 7:17am    
im getting this error
The file located at E:\Railways\App_Code\0.mp3 is not a valid wave file.
Jochen Arndt 8-Jun-18 7:21am    
Did you read my answer?

But the SoundPlayer Class can only play WAV files

The SoundPlayer does not support playing MP3 files which it tells you too by the error message.
Shahbaz435 8-Jun-18 7:33am    
yes thanks but i need 1 more help from you
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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900