1

I'm learning english and I'd like to develop a software to help me with the pronunciation.

There is a site called HowJSay, if you enter here: http://www.howjsay.com/index.php?word=car immediatly you'll hear the pronunciation of the word car . I'd like to develop a software in JAVA that could play this sound without necessity of enter in the site =]

I tried this, but doesn't work =/

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

Any Ideas? Please.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
javando
  • 139
  • 1
  • 1
  • 12

4 Answers4

4

Here you go

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

}
Marek
  • 1,878
  • 14
  • 20
  • I got a error in this code =/ javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source) at HowJSay.main(HowJSay.java:10) – javando Dec 09 '12 at 17:03
  • @javando do you run this program with argument? first argument has to be word – Marek Dec 09 '12 at 17:05
  • I put the argument. and I got the exception that I put above. It's in the line 10, first line inside the try. – javando Dec 09 '12 at 17:14
  • I'm thinking in get another way. First download the mp3 file (It's good to make some cache) and after, play the mp3 file. I'm researching about how to play a mp3 in java and it's been dificult. If someone knows how to do that, It would be helpful =] – javando Dec 09 '12 at 17:20
  • @javando, maybe you are behind proxy. – Nikolay Kuznetsov Dec 09 '12 at 17:22
  • @Nikolay Kuznetsov I'm not behind a proxy =/ I stoped the firewall too =/ – javando Dec 09 '12 at 17:32
  • @javando use my code to play from file. `getAudioInputStream` can take File as the parameter. If this does not work then problem is related to your system configuration. If it works then problem is related to connection between you and howjsay. That's just idea. – Marek Dec 09 '12 at 17:32
  • I'm downloading the new Java7 that have the javafx api, and i'll try download the mp3 and play like explained here: http://stackoverflow.com/questions/6045384/playing-mp3-and-wav-in-java – javando Dec 09 '12 at 17:36
  • @javando i'm running this code on Java 7 if that matters. Let me now how it turns out for you – Marek Dec 09 '12 at 17:40
  • @Marek I'm not understanding =/ with file I got javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1187) at HowJSay.main(HowJSay.java:15) I think that there is something wrong with my java installation – javando Dec 09 '12 at 17:41
  • @javando can't tell much from that little information. Try to use latest version of JDK and hopefully you will not have that problems. – Marek Dec 09 '12 at 17:46
  • I updated my java and keep without work =/ I'll try with the API of javafx. – javando Dec 09 '12 at 17:51
  • I got it =] Thank you for all. I followed this article: introcs.cs.princeton.edu/java/faq/mp3/MP3.java.html I'll download the mp3 and play it =] – javando Dec 09 '12 at 20:48
2

Java Sound can play short clips easily, but supports a limited number of formats out of the box. The formats it supports by default are given by AudioSystem.getAudioFileTypes() & that list will not include MP3.

The solution to the lack of support for MP3 is to add a decoder to the run-time class-path of the app. Since Java Sound works on a Service Provider Interface, it only needs to be on the class-path to be useful. An MP3 decoder can be found in mp3plugin.jar.

As to the code for playing the MP3, the short source on the info. page should suffice so long as the clips are short. Viz.

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!");
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

If you don't care much about the site then you try to use 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);
}

With help from here: Java: download Text to Speech from Google Translate

If for every word the site guarantees to have mp3 file with link howjsay.com/mp3/word.mp3 then you just need to change URL to

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

Community
  • 1
  • 1
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
0

If you're having issues with the code provided by Marek, make sure you're meeting this criteria:

  1. Use a supported audio format, such as 16 bit .wav
  2. Make sure that the URL you're using is actually playing audio automatically.

It isn't sufficient to simply reference the download page for an audio file. It has to be streaming the audio. YouTube URLs won't work, as they're videos. Audacity is a good approach to converting your audio file to a compatible 16 bit .wav file, and if you have your own domain / website, you can provide a direct link to the file from there.