简体   繁体   English

java 声音流音频的奇怪问题

[英]strange problem with java sound streaming audio

I am using a very basic java sound class to stream audio from Bing Translate for the pronounciation of Chinese characters.我正在使用来自 Bing 翻译的非常基本的 java 声音 class 到 stream 音频来发音汉字。 It's worked wonderfully for all of the 20 words I've tested except one.它对我测试过的所有 20 个单词都非常有效,除了一个。

When I try to get the pronunciation for the word 你 (which means you), I get the wrong sound.当我尝试获得单词“你”的发音时(意思是你),我得到了错误的声音。 The strange thing is, when I take the URL that's formed in the code and manually put it into a browser (I'm using the Bing Translate HTTP API), I'm getting the correct sound.奇怪的是,当我使用代码中形成的 URL 并手动将其放入浏览器时(我使用的是 Bing Translate HTTP API),我得到了正确的声音。 So it seems to me the error has to be somewhere in my code.所以在我看来,错误必须在我的代码中的某个地方。 The only place I can think of is the buffer.我唯一能想到的地方就是缓冲区。

The really strange thing is that I'm not getting silence or gibberish.真正奇怪的是,我没有得到沉默或胡言乱语。 Instead, the sound that's coming back is the way to say "one half" (literally saying "one part of two") in chinese.相反,返回的声音是用中文说“一半”(字面意思是“二分之一”)的方式。 As I said before, when I put the URL into a browser I get the proper sound for "you."正如我之前所说,当我将 URL 放入浏览器时,我会听到“你”的正确声音。

EDIT: Also, if I put 你们 (which is plural for you and includes the original character) I get the correct sound back.编辑:另外,如果我输入你们(对你来说是复数并且包括原始字符),我会得到正确的声音。

My code is below.我的代码如下。 In my pvsm, all I have is creating an instance of this class and then calling speakWord(你)在我的 pvsm 中,我所拥有的只是创建这个 class 的实例,然后调用 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;
        }
    }

}

So I finally, figured this out with some help from the microsoft translate forum.所以我终于在微软翻译论坛的帮助下解决了这个问题。 It turns out that since I'm using special characters the encoding does matter.事实证明,由于我使用的是特殊字符,因此编码确实很重要。 It's just strange how it worked for every single character except this one and this one still was able to produce some intelligible (albeit wrong) output.奇怪的是,除了这个之外,它对每个字符都有效,而这个仍然能够产生一些可理解的(尽管是错误的)output。

The following line needs to be changed from以下行需要从

URL bingTranslate = new URL(URL_FRONT + sWord + URL_END);

to

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

I only stumbled upon this because my URLs were getting a malfored URL error from the server when I attempted to play sounds from my jar file while it worked perfectly in the IDE.我只是偶然发现了这一点,因为当我尝试从我的 jar 文件中播放声音时,我的 URL 从服务器收到了错误的 URL 错误,而它在 Z581D6381F3F35E4F9D77201AC 中完美运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM