Click here to Skip to main content
15,843,865 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
need to load audio file in any format, in byte[] array which has specific audio format such as two channels and special sample rate etc.

any help will be appreciated.

What I have tried:

got this code to load a .wav file into bye[] by it just works with .wav plus I have no idea what kind of audio format that byte array has:

ByteArrayOutputStream out = new ByteArrayOutputStream();
		            BufferedInputStream in = null;
					try {in = new BufferedInputStream(new FileInputStream(A_WAVE_FILE));} 
					catch (FileNotFoundException e1) {e1.printStackTrace();}
		            int read;
		            byte[] buff = new byte[1024];
		            try {while ((read = in.read(buff)) > 0){out.write(buff, 0, read);}} 
		            catch (IOException e1) {e1.printStackTrace();}
		            try {out.flush();} catch (IOException e1) {e1.printStackTrace();}
		            byte[] audioBytes = out.toByteArray();
Posted
Updated 25-Sep-22 7:20am

That's a lot of code just to do this:
Java
byte[] buffer = File.readAllBytes(filepath);

Yeah, I get it. The entire file is going to be pulled into memory and could run the machine out of RAM. But for a test project just to learn how to parse the data, it'll do just fine.

The trick is interpreting the bytes once you have them. You need to write code for each file type you want to support. The data you get in the buffer is NOT going to be interpreted for you.
 
Share this answer
 
v2
Comments
Richard Deeming 26-Sep-22 10:30am    
The OP is using Java; wouldn't they need Files.readAllBytes instead of the .NET File.ReadAllBytes? :)
Dave Kreskowiak 26-Sep-22 10:32am    
Whoops. :shucks: Can you guess what I use all day?
You need to examine the content of the file to know which format it is. Google will find you documentation and samples on audio formats.
 
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