java声音流音频的奇怪问题

时间:2011-06-07 01:00:12

标签: java audio audio-streaming translate bing

我正在使用一个非常基本的java声音类来流式传输来自Bing Translate的音频,用于汉字的发音。除了一个,我测试过的所有20个单词都很精彩。

当我试图获得单词你的意思(这意味着你)时,我得到了错误的声音。奇怪的是,当我获取代码中形成的URL并手动将其放入浏览器(我正在使用Bing Translate HTTP API)时,我得到了正确的声音。所以在我看来,错误必须在我的代码中。我能想到的唯一的地方就是缓冲区。

真奇怪的是,我没有沉默或胡言乱语。相反,回来的声音是用中文说“一半”(字面意思是“一部分两个”)的方式。正如我之前所说,当我将URL放入浏览器时,我得到了“你”的正确声音。

编辑:此外,如果我把你们(你的复数包括原始字符)我得到了正确的声音。

我的代码如下。在我的pvsm中,我所拥有的只是创建这个类的一个实例,然后调用speakWord(你)

    public class WordSpeaker {
    private static final String TEST_CONN = "http://api.microsofttranslator.com/v2/Http.svc/" +
            "Detect?appId=5768596A4F34453BDAED3138E800D4F7EB5097B9&text=hello";
    private static final String TEST_VAL = "en";

    private static final String URL_FRONT = "http://api.microsofttranslator.com/v2/Http.svc/Speak?appId=" +
            "5768596A4F34453BDAED3138E800D4F7EB5097B9" + "&text=";
    private static final String URL_END = "&language=zh-cn";

    private AudioInputStream audioStream = null;
    private static final int EXTERNAL_BUFFER_SIZE = 128000;


    public WordSpeaker() {

    }

    public void speakWord(String sWord) {
        try {
            URL bingTranslate = new URL(URL_FRONT + sWord + URL_END);
            System.out.println(bingTranslate.toString());
            audioStream = AudioSystem.getAudioInputStream(bingTranslate);
        } catch (Exception e) {
            e.printStackTrace();
        }

        AudioFormat format = audioStream.getFormat();
        SourceDataLine line = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try {
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(format);
        } catch (LineUnavailableException le) {
            System.out.println(le);
        } catch (Exception e) {
            e.printStackTrace();
        }

        line.start();

        int bytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

        while (bytesRead != -1){
            try{
                bytesRead = audioStream.read(abData,0,abData.length);
            } catch (Exception e){
                e.printStackTrace();
            }
            if (bytesRead >=0){
                int bytesWritten = line.write(abData,0,bytesRead);
            }
        }

        line.drain();
        line.close();

    }

    private boolean testBing() {
        try {
            URL test = new URL(TEST_CONN);
            BufferedReader testRead = new BufferedReader(new InputStreamReader(test.openStream()));

            String inLine = testRead.readLine();
            return inLine.substring(inLine.lastIndexOf("\">") + 2, inLine.lastIndexOf("<")).equals(TEST_VAL);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}

1 个答案:

答案 0 :(得分:1)

所以我终于在微软翻译论坛的帮助下想出了这个。事实证明,由于我使用特殊字符,编码确实很重要。奇怪的是它如何对除了这一个之外的每一个角色起作用,这个仍然能够产生一些可理解的(尽管是错误的)输出。

需要从

更改以下行
URL bingTranslate = new URL(URL_FRONT + sWord + URL_END);

URL bingTranslate = new URL(URL_FRONT + URLEncoder.encode(sWord, "UTF-8") + URL_END);

我只是偶然发现了这一点,因为当我尝试在我的jar文件中播放声音时,我的URL从服务器获得了一个malfored URL错误,而它在IDE中完美运行。