I am trying to call Whisper API from my android app. I am recording the audio and storing in .wav
format. when i am calling the whisper API its always giving me 400 error saying invalid file format.
I tried playing the audio back and its working. also file is readable. have been trying to find solution for couple of hours now.
following is my code
private void startRecording() {
fileName = getExternalCacheDir().getAbsolutePath() + "/recording.wav";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setOutputFile(fileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
}
private void stopRecording() {
recorder.stop();
recorder.release();
recorder = null;
// Copy the recorded WAV file to a new file
String newFileName = getExternalCacheDir().getAbsolutePath() + "/new_recording.wav";
try {
Files.copy(Paths.get(fileName), Paths.get(newFileName), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
OkHttpClient client = new OkHttpClient();
File file = new File(newFileName);
String absolutePath = file.getAbsolutePath();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "new_recording.wav", RequestBody.create(MediaType.parse("audio/wav"), new File(absolutePath)))
.addFormDataPart("model", "whisper-1")
.build();
Request request = new Request.Builder()
.url("https://api.openai.com/v1/audio/transcriptions")
.post(requestBody)
.header("Authorization", "Bearer sk-QAym*******gUIRdxXGq")
.header("Content-Type", "multipart/form-data")
.build();
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
if (!response.isSuccessful()) {
System.out.println("Unexpected code " + response);
}
System.out.println(response.body().string());
}
});
}
was just trying to create another wav file from existing, and this is also not working.
would appreciate any help or suggestions.
thanks