我正在尝试创建一个网页,用户可以在其中收听他输入的文字。我还想提供以.wav
格式下载媒体的功能。
我可以从AudioStream
获取AmazonPollyClient
但无法将流保存到.wav
文件。
这是我正在尝试的......
public void convertToWav(SynthesizeSpeechResult result, File convertedFile) throws IOException {
try (AudioInputStream sias = AudioSystem.getAudioInputStream(result.getAudioStream())){
AudioFormat newFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000f, 16, 1, 2, 8000f, false);
AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(newFormat, sias);
AudioSystem.write(convert1AIS, AudioFileFormat.Type.WAVE, convertedFile);
convert1AIS.close();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.gc();
}
但每当我致电getAudioInputStream()
时,它都会抛出IOException
java.io.IOException: mark/reset not supported
at java.io.InputStream.reset(InputStream.java:348)
at com.amazonaws.internal.SdkFilterInputStream.reset(SdkFilterInputStream.java:112)
at com.amazonaws.event.ProgressInputStream.reset(ProgressInputStream.java:168)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:139)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1113)
P.S:我尝试在result.getAudioStream()
中包裹BufferedInputStream()
。没有秀!
任何帮助将不胜感激。
答案 0 :(得分:0)
最后,我找到了保存InputStream
polly-aws-sdk
的方法
public static boolean getWavFile(String filePath, InputStream audioStream) throws IOException {
boolean result = false;
try {
byte[] decodedData = IOUtils.toByteArray(audioStream);
System.out.println(">>Decoded Data" + Arrays.toString(decodedData));
File outFile = new File(filePath);
AudioFormat format = new AudioFormat(8000, 16, 1, true, false);
AudioSystem.write(new AudioInputStream(new ByteArrayInputStream(
decodedData), format, decodedData.length), AudioFileFormat.Type.WAVE, outFile);
result = true;
return result;
} catch (IOException ex) {
System.out.println("<<getWavFile - impl" + ex);
return result;
}
}