我没有收到Watson TTS的音频

时间:2017-12-13 12:11:49

标签: watson

这是我的java代码:

    import java.io.File; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.OutputStream;

    import com.ibm.watson.developer_cloud.text_to_speech.v1.TextToSpeech; 
    import com.ibm.watson.developer_cloud.text_to_speech.v1.model.AudioFormat; 
    import com.ibm.watson.developer_cloud.text_to_speech.v1.model.Voice; 

    public static void main(String[] args) {

     TextToSpeech textService = new TextToSpeech(IBM_WATSON_USERNAME, IBM_WATSON_PASSWORD);
     String text = "Show me the meaning of being lonely";

     try {
         InputStream in = textService.synthesize(text, Voice.EN_ALLISON, AudioFormat.WAV)
                 .execute();
         System.out.println(in.available());
         byte[] buffer = new byte[in.available()];
         in.read(buffer);

         File targetFile = new File("local_path/Aud.wav");
         OutputStream outStream = new FileOutputStream(targetFile);
         outStream.write(buffer);
         outStream.close();

     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
    }

当我运行代码时,我在Eclipse Console中获得响应:

Dec 13, 2017 5:38:35 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_AllisonVoice&accept=audio/wav http/1.1 (71-byte body)
Dec 13, 2017 5:38:35 PM okhttp3.internal.platform.Platform log
INFO: <-- 200 OK https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_AllisonVoice&accept=audio/wav (501ms, unknown-length body)

in.available()的值为0
我没有生成音频。因此,根据代码流程,我得到一个0kB的.wav文件生成。我没有什么音频?

1 个答案:

答案 0 :(得分:1)

尝试这个简单的方法

public class tts
{
    public static void main( String[] args ) throws Exception
    {
        TextToSpeech service = new TextToSpeech();
        service.setUsernameAndPassword();

        String text = "Show me the meaning of being lonely";    
        InputStream stream =service.synthesize(text, Voice.EN_ALLISON, AudioFormat.WAV).execute();
            AudioPlayer.player.start(WaveUtils.reWriteWaveHeader(stream));
        }}
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}