我正在使用 ElevenLabs Api 将文本转换为语音,这个 api 主要返回 byte[] 作为音频,我尝试使用 MediaPlayer 和 AudioTrack 播放它但失败了,有没有人如何做到这一点,提前谢谢
[-61, -65, -61, -69, 80, -61, -124, 0, 0, 7, 40, 1, 103, -62, -76, 17, -62, -128, 1, -61, -113, -62, -99, -61, -83, 115, 24, -62, -80, 0, 0, 57, 38, 104, 0, -61, -108, 47, -61,
private void elevenLab() throws JSONException {
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, LAB_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
// getting response as byte
playFile(response.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
Log.d("VALUE","Response " + Arrays.toString(response.getBytes()));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("TAG","Error is " + error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("accept", "audio/mpeg");
headers.put("xi-api-key", LAB_KEY);
headers.put("Content-Type", "application/json");
return headers;
}
@Override
public byte[] getBody() {
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("text", "Hello How are you man?");
JSONObject voiceSettings = new JSONObject();
voiceSettings.put("stability", 0);
voiceSettings.put("similarity_boost", 0);
jsonBody.put("voice_settings", voiceSettings);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonBody.toString().getBytes();
}
};
requestQueue.add(stringRequest);
}
private void playFile(byte[] response) throws IOException {
int sampleRate = 44100;
int channelConfig = AudioFormat.CHANNEL_OUT_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int bufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat);
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, bufferSize, AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(response, 0, response.length);
}
这段代码的主要问题是你在请求和操作字符串,而返回的对象是byte[]。
您可能应该使用 ByteArrayRequest 而不是 StringRequest。 这是对您的代码的建议更改:
// Request a byte array response from the provided URL.
Request<byte[]> request = new Request<byte[]>(Request.Method.POST, LAB_URL, new Response.Listener<byte[]>() {
@Override
public void onResponse(byte[] response) {
try {
Log.d("TAG","Response : " + response.length);
playFile(response);
} catch (IOException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("TAG","Error is " + error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("accept", "audio/mpeg");
headers.put("xi-api-key", LAB_KEY);
headers.put("Content-Type", "application/json");
return headers;
}
@Override
public byte[] getBody() {
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("text", "Good Luck");
JSONObject voiceSettings = new JSONObject();
voiceSettings.put("stability", 0);
voiceSettings.put("similarity_boost", 0);
jsonBody.put("voice_settings", voiceSettings);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonBody.toString().getBytes();
}
@Override
protected Response<byte[]> parseNetworkResponse(NetworkResponse response) {
// Returns the raw data received from the network without any conversion.
return Response.success(response.data, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(byteRequest);
请注意,这不会检查响应是否为 200。根据他们的规范,如果请求有问题,它将返回 application/json 内容。