Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to play audio sound that was sent to MySQL DB into a column called "CUST_FILE" with datatype of BLOB but apparently I got an exception saying "THe wave header is corrupt" on WinForms and I don't know why..

This is how the sound was sent to database (I selected .wav file in this case):

Byte[] toByte_ = File.ReadAllBytes(open.FileName);

    String insertTxtQuery = "INSERT INTO " + nameTable + "(CUST_USERNAME,CUST_FILE,CUST_FILE_PATH) VALUES (@CUST_USERNAME,@CUST_FILE,@CUST_FILE_PATH)";

command = new MySqlCommand(insertTxtQuery, con);

                        command.Parameters.Add("@CUST_USERNAME",MySqlDbType.Text);
command.Parameters["@CUST_USERNAME"].Value = label5.Text;
               
command.Parameters.Add("@CUST_FILE_PATH",MySqlDbType.Text);
command.Parameters["@CUST_FILE_PATH"].Value = getName;

command.Parameters.Add("@CUST_FILE",MySqlDbType.LongBlob);
command.Parameters["@CUST_FILE"].Value = toByte_;
command.ExecuteNonQuery();


What I have tried:

String _selectAud = "SELECT CUST_FILE FROM file_info_audi WHERE CUST_USERNAME = @username AND CUST_FILE_PATH = @filename";
command = new MySqlCommand(_selectAud, con);
command.Parameters.AddWithValue("@username", Form1.instance.label5.Text);
command.Parameters.AddWithValue("@filename", label1.Text);


MySqlDataReader _dr = command.ExecuteReader();

DataTable _DT = new DataTable();
_DT.Load(_dr);
Byte[] audioSetup_ = (byte[])_DT.Rows[0][0];

_dr.Close();

byte[] sound = ASCIIEncoding.Default.GetBytes(audioSetup_.ToString());
using(MemoryStream _ms = new MemoryStream(sound)) {
    System.Media.SoundPlayer player = new System.Media.SoundPlayer(_ms);
    player.Load();
    player.Play();
}
Posted
Updated 2-Nov-22 2:31am
v7

You're making a few mistakes. You're storing the filepath of the audio file AND THE FILE CONTENT in the database. Why are you storing the filepath if you're also storing the content? The solution is to NOT store the content in the database and just depend on the filepath to point to the correct file on the server.

WHy on earth are you converting the bytes from the returned record to a STRING? Why then are you using ASCII encoding to get modified(!) bytes back from that string? You're only corrupting the audio data when you do those two operations! You don't need that line of code at all! The audioSetup_ variable is going to have the bytes you need to play.
 
Share this answer
 
Comments
Dan Sep2022 3-Nov-22 8:15am    
"Why are you storing the filepath if you're also storing the content?" I have to make a software that displays the path location of the content along with the content itself. For your second comment, I'll make sure to avoid that mistake in the future...
Dave Kreskowiak 3-Nov-22 8:25am    
Yeah, and you do NOT need to store the content in the database. Since you have the filepath stored in the database, you only need to keep the file in an accessible folder. When you return the data from the database, you can easily open the file, grab the content, and send that back to the client. You reduce the database size greatly and increase its performance since it doesn't have to return more data than necessary.
Start by checking how you added the data to the DB: unless it is correctly stored you can't use it.

The most common mistake with images or sound samples is to try and concatenate strings to form the SQL INSERT statement - see here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]

If you have checked that and are happy that the data is definitely the right length and contains valid sound info, then use the debugger to find out exactly what the returned data looks like, and compare that to the original file content (using a hex editor)

Sorry, but we can't do any of that for you!
 
Share this answer
 
Comments
Dan Sep2022 2-Nov-22 8:08am    
How do I use debugger to find out what the returned data looks like?
OriginalGriff 2-Nov-22 8:27am    
You don't know how to use a debugger? :omg:
How have you got this far without learning that?

Google will help you get started:

https://www.google.com/search?q=visual+studio+debugger&oq=visual+studio+debugger&aqs=chrome..69i57j0i512l9.8022j0j7&sourceid=chrome&ie=UTF-8
Dan Sep2022 3-Nov-22 7:53am    
Apparently, the debugger return no value.
Dave Kreskowiak 3-Nov-22 8:27am    
The debugger will never return a value. That's not what it does. It allows you to inspect the data in variables while your code is running (and paused).

Now, if you're saying you've got variable that don't have appropriate data in them, you have to use that information to trace back in your code how and why that happened, and correct your code accordingly.
OriginalGriff 3-Nov-22 8:55am    
The debugger is a tool which allows you to look at what your app is doing while it is running; to pause it and look at it's variables; to run it line-by-line and watch where it goes and why.

Pretty much all the links in the Google search I gave you yesterday explain this, and how to use teh debugger to help you fix your code ...

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