2

我正在尝试创建一个网页,用户可以在其中收听他键入的文本。我还想提供一种以.wav格式下载媒体的功能。

我可以从中获取AudioStreamAmazonPollyClient无法将流保存到.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)

PS:我试过换result.getAudioStream()BufferedInputStream()。没有出现!

任何帮助将不胜感激。

4

1 回答 1

1

最后,我想出了保存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;

    }
}
于 2017-11-15T18:42:10.260 回答