user3839386
user3839386

Reputation: 1

Converting raw byte data to audio

In my application, I want to change the raw byte data to the audio. So, at first I receive the data and add it to a byte array list and than I want to change the whole array list to the audio. As the data is raw through audio jack, I would appreciate if you let me know which audio format is suitable (wav,3gp and so on)? Also, I have a problem with saving this data in a file. This is my code. in line of fileOuputStream.write( audiod.toArray()), it give an error and asked me to change the type of audiod to byte.

private static ArrayList<Byte> audiod = new ArrayList<Byte>();
audioFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myaudio.wav";

if (dg == 1) {
    //audiod.append(data[i]);
    for (int i = 0; data != null && i < data.length; i++) {
        audiod.add(data[i]);
    }
    if (stt.equalsIgnoreCase(("r"))) {
        dg = 0;
        recordButton.setEnabled(true);
        File file = new File(audioFilePath);
        try {
            FileOutputStream fileOuputStream = new FileOutputStream(audioFilePath);
            fileOuputStream.write( audiod.toArray());
            fileOuputStream.close();
            System.out.println("Done");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Views: 649

Answers (1)

Henry
Henry

Reputation: 43728

audiod.toArray() will give you a Byte[], however you need a byte[]. In Java there are so called wrapper classes for the primitive data types. A Byte is an object that holds byte values.

Why do you use a list of Byte at all? It needs a lot more memory than a simple byte[] you seem to have anyhow (data).

The data format (wav,3gp and so on) depends on how you got your data.

Upvotes: 1

Related Questions