2017-12-12 13 views
0

을이 내 자바 코드 생성하지 : 이클립스의 코드를 실행하면왓슨 텍스트 오디오 출력

public static void main(String[] args) { 

    TextToSpeech textService = new TextToSpeech(IBM_WATSON_USERNAME, IBM_WATSON_PASSWORD); 

    //String voice = "en-US_AllisonVoice"; 
    String text = "This is Just awesome And i am going to experience the effect"; 
    //String format = "audio/mp3"; 

    try { 
     InputStream in = textService.synthesize(text, Voice.EN_ALLISON, AudioFormat.OGG_VORBIS) 
       .execute(); 
     System.out.println(in.available()); 

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

}

, 난 점점 오전 :

Dec 12, 2017 3:05:08 PM okhttp3.internal.platform.Platform log 
INFO: --> POST https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_AllisonVoice&accept=audio/ogg;%20codecs%3Dvorbis http/1.1 (71-byte body) 
Dec 12, 2017 3:05:09 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/ogg;%20codecs%3Dvorbis (588ms, unknown-length body) 

출력을위한을 in.available() : 0

왜 오디오가 들리지 않습니까? POST 요청에 따라 텍스트가 게시되지 않는 것을 볼 수 있습니다. 무엇이 누락 되었습니까?

+0

나를 도와 줄 수있는 사람이 있니? –

답변

0

InputStreamavailable() 메서드는 InputStream의 구현에 따라 항상 0을 반환합니다. InputStream Javadoc을 참조하십시오.

synthesize()을 호출 할 때받는 InputStream은 okHttp 라이브러리의 byteStream()입니다.

InputStream에서 파일이나 다른 곳으로 읽어야합니다. 는 여기에 사용할 수있는 코드입니다 :

inputStream = textToSpeech.synthesize(/* parameters*/).execute(); 
outputStream = new FileOutputStream(new File("audio.ogg")); 
int read = 0; 
byte[] bytes = new byte[1024]; 
while ((read = inputStream.read(bytes)) != -1) { 
    outputStream.write(bytes, 0, read); 
} 
System.out.println("Done!"); 

참고 :이 조각은 위의 모든 try{}catch을 가지고 있지 않으며,이 스트림을 닫지 않습니다. 나는 그걸 너에게 맡길거야 =)