Karun Dawadi
Karun Dawadi

Reputation: 53

How to read .wav file as byte array in android studio?

I am using Java in android studio to write my application. I have a file path to an audio file (in .wav format), What would be the best method to read the .wav file into a byte array? I am using the following conversion method:

public void open_audio_file(Uri filePath){
        try{
            in = new BufferedInputStream(getContext().getContentResolver().openInputStream(filePath));

            int read;
            byte[] buff = new byte[1024];
            while ((read = in.read(buff)) > 0)
            {
                out.write(buff, 0, read);
            }
            out.flush();
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        //Todo : Change the audio file to a float pointer
        audioBytes = out.toByteArray();
        Log.i(LOG_TAG,"The audio file is " + audioBytes.toString());
    }

However, this method produces a lot of jargon values that are not read by my function. For example, the output of dog.wav www.beaniebestbuy.com/sounds/dog5.wav was [B@d25663f . There are jargon values attached to the beginning of the array.

Update 1 : I tried to use the AudioSystem library, but it is not supported in Android studio.

Upvotes: 2

Views: 1114

Answers (1)

TechnoSam
TechnoSam

Reputation: 578

You're reading the file correctly. Since you're just reading bytes, the file type is actually irrelevant.

When you call toString on an array, it doesn't give you a representation of the entire array, it just gives you the object reference. See documentation here.

If you want to see the full array, you'll need to use a for loop.

for(int i = 0; i < audioBytes.length; i++) {
    System.out.print(audioBytes[i] +" ");
}

Alternatively, you can use Arrays.toString()

Do note that this will probably be a very large output, not likely to be easily readable.

Upvotes: 1

Related Questions