Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all , i am trying to create .wav type audio file with bytes, actually i have .m4a format file i read the file into bytes then i create a another .wav format file, with below code
C#
var bytes = File.ReadAllBytes(Server.MapPath("Audios/Breakfast.m4a"));      
        using (FileStream fs = File.Create(@"C:\Docs\myFile.wav"))
            {
                fs.Write(bytes, 0, bytes.Length);
            }

and the file is created, but cannot play. if i try to read the created file(myFile.wav) by using Naudio dll with below code
var myReader = new WaveFileReader(@"C:\Docs\myFile.wav");

it show below Error Message
System.FormatException: Not a WAVE file - no RIFF header
here how can i Create .wav files without any issues by using Bytes ?
TIA

What I have tried:

C#
var bytes = File.ReadAllBytes(Server.MapPath("Audios/Breakfast.m4a"));     
       using (FileStream fs = File.Create(@"C:\Docs\myFile.wav"))
           {
               fs.Write(bytes, 0, bytes.Length);
           }
Posted
Updated 7-Nov-16 1:50am
v2

That won't work.
Changing the file extension does not affect the data - the information in the "myFile.wav" file is identical to the information in the "Breakfast.m4a" file. Which means it's a valid M4A file, but an invalid WAV file - so the WAV file reader can't process it.
It's a bit like prying the Ford badges off your car and fitting Ferrari ones - it's not going to fool anyone into thinking it's a sports car!

To change m4a files to wav you have to change the format, which is not a trivial process. I'd suggest you find a pre-built converter and use that as a command line tool to do the conversion.
 
Share this answer
 
Comments
#realJSOP 7-Nov-16 7:55am    
No self-respecting Ford owner would ever put Ferrari badges on their car.

I think FFMPEG will do what he wants.
You can't just dump some bytes into a file and expect that they magically become something else.

Do some research on the source data format (M4A) and the destination format (WAV), learn how to extract the raw audio data from the source, convert it and then write a valid file in the destination format.

Here is a link to some information on the WAV format: RIFF WAVE – Wikipedia[^]
 
Share this answer
 
v2
Comments
#realJSOP 7-Nov-16 7:56am    
"You can't just dump some bytes into a file and expect that they magically become something else."

Hillary Clinton seems to be able to pull it off...
[no name] 7-Nov-16 8:01am    
This time your election has become a choice between Scylla and Charybdis.

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