使用Java从URL获取声音

我正在学习英语,我想开发一个软件来帮助我发音。

有一个名为HowJSay的网站,如果你进入这里: http : //www.howjsay.com/index.php ?word = car immediatly你会听到汽车这个词的发音。 我想开发一个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); } 

有任何想法吗? 请。

干得好

 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) { } } } } } 

如果您对网站不太关心,那么您尝试使用Google Translate 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:从Google Translate下载Text to Speech

如果对于每个word网站保证有mp3文件链接howjsay.com/mp3/word.mp3那么你只需要更改URL到

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

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!"); } }); } } 

如果您遇到Marek提供的代码问题,请确保您符合以下条件:

  1. 使用支持的音频格式,例如16位.wav
  2. 确保您使用的URL实际上是自动播放音频。

仅仅引用音频文件的下载页面是不够的。 它必须是流式传输音频。 YouTubeurl无法使用,因为它们是video。 Audacity是将音频文件转换为兼容的16位.wav文件的好方法,如果您有自己的域/网站,则可以从那里提供指向该文件的直接链接。