简体   繁体   中英

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();
        }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM