I have a problem with a Android app that call ACRCloud. For the work that I need to do, I want to save the recording in a MP3 file but I don't know why it doesn't work.
That's my code :
File f = null;
try {
f = createAudioFile();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = acrCloudResult.getRecordDataPCM();
if(f.exists()){
String path = f.getPath();
FileOutputStream stream = null;
try {
stream = new FileOutputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
stream.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
The format of PCM audio data from "acrCloudResult.getRecordDataPCM()" is "PCM 16 bit, mono 8000 Hz", and this audio data buffer do not include "Audio Header".
You can add a wav header into PCM audio data, can refer to the following code "writeWaveFileHeader", this step can convert PCM to WAV, and you can play this wav file with audio player.
writeWaveFileHeader(stream, data.length, data.length+36, 8000, 1, 2000);
You can use a third-party library to convert PCM/WAV to MP3. For Example: Lame, and you can refer to this code, https://github.com/Jay-Goo/Mp3Converter.