Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a resource sound file into a Byte array

I have cheerapp.wav or cheerapp.mp3 or some other format.

InputStream in = context.getResources().openRawResource(R.raw.cheerapp);       
BufferedInputStream bis = new BufferedInputStream(in, 8000);
// Create a DataInputStream to read the audio data from the saved file
DataInputStream dis = new DataInputStream(bis);

byte[] music = null;
music = new byte[??];
int i = 0; // Read the file into the "music" array
while (dis.available() > 0) {
    // dis.read(music[i]); // This assignment does not reverse the order
    music[i]=dis.readByte();
    i++;
}

dis.close();          

For the music byte array which takes the data from the DataInputStream. I don't know what the length of that to allocate.

This is raw file from resource not a file therefore I wouldn't know the size of that thing.

like image 901
jason white Avatar asked Feb 26 '13 20:02

jason white


2 Answers

You do have byte array length as you can see:

 InputStream inStream = context.getResources().openRawResource(R.raw.cheerapp);
 byte[] music = new byte[inStream.available()];

And then you can read whole Stream into byte array easily.

Of course I would recommend that you do check when it comes to the size and use ByteArrayOutputStream with smaller byte[] buffer if needed:

public static byte[] convertStreamToByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buff = new byte[10240];
    int i = Integer.MAX_VALUE;
    while ((i = is.read(buff, 0, buff.length)) > 0) {
        baos.write(buff, 0, i);
    }

    return baos.toByteArray(); // be sure to close InputStream in calling function
}

If you'll be doing lots of IO operations I recommend that you make use of org.apache.commons.io.IOUtils. That way you won't need to worry too much about quality of your IO implementation and once you import JAR into your project you would just do:

byte[] payload = IOUtils.toByteArray(context.getResources().openRawResource(R.raw.cheerapp));
like image 152
nikib3ro Avatar answered Sep 19 '22 13:09

nikib3ro


Hope it will help.

Create an sdcard path:

String outputFile = 
    Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";

Convert as a file and have to call the byte array method:

byte[] soundBytes;

try {
    InputStream inputStream = 
        getContentResolver().openInputStream(Uri.fromFile(new File(outputFile)));

    soundBytes = new byte[inputStream.available()];
    soundBytes = toByteArray(inputStream);

    Toast.makeText(this, "Recordin Finished"+ " " + soundBytes, Toast.LENGTH_LONG).show();
} catch(Exception e) {
    e.printStackTrace();
}

method:

public byte[] toByteArray(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int read = 0;
    byte[] buffer = new byte[1024];
    while (read != -1) {
        read = in.read(buffer);
        if (read != -1)
            out.write(buffer,0,read);
    }
    out.close();
    return out.toByteArray();
}
like image 29
Mahendran Candy Avatar answered Sep 17 '22 13:09

Mahendran Candy