2012-12-09 34 views
0

我正在學習英語,我想開發一個軟件來幫助我發音。從Java的URL獲取聲音

有一個網站叫HowJSay,如果你在這裏輸入:http://www.howjsay.com/index.php?word=car 立即你會聽到汽車這個詞的發音。我想用Java開發一個軟件,可以玩這個聲音能夠在網站上輸入的必要性=]

我想這一點,但沒有工作=/

public static void main(String[] args) throws Exception { 
    URL url = new URL("http://www.howjsay.com/index.php?word=car"); 
    url.openConnection(); 
    AudioStream as = new AudioStream(url.openStream()); 
    AudioPlayer.player.start(as); 
    AudioPlayer.player.stop(as); 
} 

什麼想法?請。

+1

如何不起作用?例外或沒有任何反應?你使用的構造函數看起來不正確。 –

+1

你是如何期待這個url返回一個聲音文件的? – vels4j

+3

其實,真正的網址是:http://www.howjsay.com/mp3/car.mp3 – Marek

回答

1

如果你不那麼在意的網站,那麼你嘗試使用谷歌翻譯API

try{ 
     String word="car"; 
     word=java.net.URLEncoder.encode(word, "UTF-8"); 
     URL url = new URL("http://translate.google.com/translate_tts?ie=UTF-8&tl=ja&q="+word); 
     HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
     urlConn.addRequestProperty("User-Agent", "Mozilla/4.76"); 
     InputStream audioSrc = urlConn.getInputStream(); 
     DataInputStream read = new DataInputStream(audioSrc); 
     AudioStream as = new AudioStream(read); 
     AudioPlayer.player.start(as); 
     AudioPlayer.player.stop(as); 
} 

的幫助從這裏: Java: download Text to Speech from Google Translate

如果每word網站保證有MP3與鏈接howjsay.com/mp3/word.mp3文件,那麼你只需要改變URL到

URL url = new URL("howjsay.com/mp3/" + word + ".mp3");

3

在這裏你去

import javax.sound.sampled.*; 
import java.io.IOException; 
import java.net.URL; 

public class HowJSay 
{ 
public static void main(String[] args) { 
    AudioInputStream din = null; 
    try { 
     AudioInputStream in = AudioSystem.getAudioInputStream(new URL("http://www.howjsay.com/mp3/"+ args[0] +".mp3")); 
     AudioFormat baseFormat = in.getFormat(); 
     AudioFormat decodedFormat = new AudioFormat(
       AudioFormat.Encoding.PCM_SIGNED, 
       baseFormat.getSampleRate(), 16, baseFormat.getChannels(), 
       baseFormat.getChannels() * 2, baseFormat.getSampleRate(), 
       false); 
     din = AudioSystem.getAudioInputStream(decodedFormat, in); 
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat); 
     SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); 
     if(line != null) { 
      line.open(decodedFormat); 
      byte[] data = new byte[4096]; 
      // Start 
      line.start(); 

      int nBytesRead; 
      while ((nBytesRead = din.read(data, 0, data.length)) != -1) { 
       line.write(data, 0, nBytesRead); 
      } 
      // Stop 
      line.drain(); 
      line.stop(); 
      line.close(); 
      din.close(); 
     } 

    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    finally { 
     if(din != null) { 
      try { din.close(); } catch(IOException e) { } 
     } 
    } 
} 

} 
+0

我在這段代碼中有一個錯誤=/javax.sound.sampled.UnsupportedAudioFileException:無法從輸入URL獲得音頻輸入流 \t在javax.sound.sampled.AudioSystem.getAudioInputStream(來源不明) \t在HowJSay.main( HowJSay.java:10) – javando

+0

什麼行?我只是複製並粘貼它,它適用於我。 – Marek

+0

@javando你用參數運行這個程序嗎?第一個參數必須是字 – Marek

0

Java Sound可以輕鬆播放短片,但支持的格式數量有限的開箱即用。默認支持的格式爲AudioSystem.getAudioFileTypes() &該列表不包含MP3。

缺少對MP3支持的解決方案是將解碼器添加到應用程序的運行時類路徑。由於Java Sound在一個服務提供者接口上工作,它只需要在類路徑上有用。 MP3解碼器可在mp3plugin.jar中找到。

至於播放MP3的代碼,信息的簡短來源。只要剪輯短,頁面就足夠了。即

import java.net.URL; 
import javax.swing.*; 
import javax.sound.sampled.*; 

public class LoopSound { 

    public static void main(String[] args) throws Exception { 
     URL url = new URL(
      "http://pscode.org/media/leftright.wav"); 
     Clip clip = AudioSystem.getClip(); 
     // getAudioInputStream() also accepts a File or InputStream 
     AudioInputStream ais = AudioSystem. 
      getAudioInputStream(url); 
     clip.open(ais); 
     clip.loop(Clip.LOOP_CONTINUOUSLY); 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       // A GUI element to prevent the Clip's daemon Thread 
       // from terminating at the end of the main() 
       JOptionPane.showMessageDialog(null, "Close to exit!"); 
      } 
     }); 
    } 
} 
0

如果您有與馬立克提供的代碼問題,請確保您滿足此條件:

  1. 使用支持的音頻格式,如16位的.wav
  2. 確保您使用的網址實際上是自動播放音頻。

僅僅引用音頻文件的下載頁面是不夠的。它必須流式傳輸音頻。 YouTube網址無效,因爲它們是視頻。 Audacity是將音頻文件轉換爲兼容的16位.wav文件的好方法,如果您擁有自己的域名/網站,則可以從該文件直接鏈接到該文件。