我有一个 mp3 文件 hello.mp3。我将 mp3 包装到 FileInputStream 并将输入流转换为字节,然后将字节推送到 SynthesisCallback.audioAvailable(bytes,offset,length) 但这结果只是噪声。如果我将文件 hello.mp3 加载到我的 Android 音乐播放器。
为什么当我将字节从文件推送到 SnthesisCallback 时这不起作用?我在下面粘贴了我的代码。
这是我从 mp3 文件生成音频流的地方:
class AudioStream {
InputStream stream;
int length;
}
private AudioStream getAudioStream(String text) throws IOException {
// TODO parse text, and generate audio file.
File hello = new File(Environment.getExternalStorageDirectory(), "hello.mp3");
AudioStream astream = new AudioStream();
astream.length = hello.length();
astream.stream = new FileInputStream(hello);
return astream;
}
这是我的 Inputstream to byte[] 方法。
public byte[] inputStreamToByteArray(AudioStream inStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[inStream.length];
int bytesRead;
while ((bytesRead = inStream.stream.read(buffer)) > 0) {
baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
}
这是我的 TextToSpeechService
类中的 onSynthesizeText
方法。
@Override
protected synchronized void onSynthesizeText(SynthesisRequest request,
SynthesisCallback callback) {
// TODO load language and other checks.
// At this point, we have loaded the language
callback.start(16000,
AudioFormat.ENCODING_PCM_16BIT, 1 /* Number of channels. */);
final String text = request.getText().toLowerCase();
try {
Log.i(TAG, "Getting audio stream for text "+text);
AudioStream aStream = getAudioStream(text);
byte[] bytes = inputStreamToByteArray(aStream);
final int maxBufferSize = callback.getMaxBufferSize();
int offset = 0;
while (offset < aStream.length) {
int bytesToWrite = Math.min(maxBufferSize, aStream.length - offset);
callback.audioAvailable(bytes, offset, bytesToWrite);
offset += bytesToWrite;
}
} catch (Exception e) {
e.printStackTrace();
callback.error();
}
// Alright, we're done with our synthesis - yay!
callback.done();
}
这就是我测试我的合成引擎的方式。
//initialize text speech
textToSpeech = new TextToSpeech(this, new OnInitListener() {
/**
* a callback to be invoked indicating the completion of the TextToSpeech
* engine initialization.
*/
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("error", "Language is not supported");
} else {
convertToSpeech("Hello");
}
} else {
Log.e("error", "Failed to Initilize!");
}
}
/**
* Speaks the string using the specified queuing strategy and speech parameters.
*/
private void convertToSpeech(String text) {
if (null == text || "".equals(text)) {
return;
}
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
});
最佳答案
函数 audioAvailable(byte[] buffer, int offset, int length)
期望 PCM 样本作为输入。您不能从 .mp3 文件中读取字节并将其用作函数的输入。您需要使用 .wav 文件或先将 .mp3 文件转换为 .wav 文件并将其用作 audioAvailable
函数的输入。
关于java - 来自音频文件的 Android 基本 TTS 引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25421066/