Topic
5 replies Latest Post - ‏2011-10-15T00:11:19Z by zabalex
SystemAdmin
SystemAdmin
4672 Posts
ACCEPTED ANSWER

Pinned topic Playing a sound file through java program.

‏2008-10-16T11:35:13Z |
Hi! All

I have a requirement in my java application that is i have to play sound files of different formats. Pls tell me is there an API and how to write the code.

Thanks in advance
Updated on 2011-10-15T00:11:19Z at 2011-10-15T00:11:19Z by zabalex
  • SystemAdmin
    SystemAdmin
    4672 Posts
    ACCEPTED ANSWER

    Re: Playing a sound file through java program.

    ‏2008-10-16T23:23:50Z  in response to SystemAdmin
    File file = new File("bark.wav");
    AudioClip clip = Applet.newAudioClip(file.toURL());
    clip.play();

    is the basic way to play a file

    You can also check out the Java Media Framework API for additional features.
  • FritzKatz
    FritzKatz
    1 Post
    ACCEPTED ANSWER

    Re: Playing a sound file through java program.

    ‏2011-02-22T20:42:52Z  in response to SystemAdmin
    I'm developing a stand-alone application, not an applet.

    I'd like to load an audio clip and play it multiple times in response to a button press events.
    Application is a piano simulator, with 88 sampled .wav files.

    Using JMF, I can easily load the audio file and play it once, it doesn't play the second time.

    Here's the SimpleAudioPlayer.java modified to show the problem. What am I doing wrong, and
    how can I fix it? (Please be gentle to me, I'm a newbie :-).

    ____________________________________

    import javax.media.*;

    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    import java.net.MalformedURLException;

    /**
    * A Simple audio player class for playing audio files using JMF.
    * This class is able to play any audio formats that the JMF supports,
    * including (but not limited to:) mp3, rmf, aiff, wav, au, and midi.
    * See java.sun.com/products/java-media/jmf/2.1.1/formats.html for
    * more information on supported file formats in JMF.
    */
    public class SimpleAudioPlayer {

    /**
    * The Player object which is used to play an audio file.
    */
    private Player audioPlayer = null;

    /**
    * Creates a new instance of the simple audio player.
    * @param url The url of the audio file to play.
    * @throws IOException Indicates a problem opening the file.
    * @throws NoPlayerException JMF was unable to create a player for
    * the file type specified.
    * @throws CannotRealizeException Indicates a problem in realizing the
    * player.
    */
    public SimpleAudioPlayer(URL url) throws IOException, NoPlayerException,
    CannotRealizeException {
    audioPlayer = Manager.createRealizedPlayer(url);
    }

    /**
    * Creates a new instance of the simple audio player.
    * @param path The Resource(path) of the audio file to play.
    * @throws IOException Indicates a problem opening the file.
    * @throws NoPlayerException JMF was unable to create a player for
    * the file type specified.
    * @throws CannotRealizeException Indicates a problem in realizing the
    * player.
    */
    public SimpleAudioPlayer(String path) throws IOException, NoPlayerException,
    CannotRealizeException {
    java.net.URL audioURL = getClass().getResource(path);
    audioPlayer = Manager.createRealizedPlayer(audioURL);
    }

    /**
    * Creates a new instance of the simple audio player.
    * @param file The audio file to play.
    * @throws IOException Indicates a problem opening the file.
    * @throws NoPlayerException JMF was unable to create a player for
    * the file type specified.
    * @throws CannotRealizeException Indicates a problem in realizing the
    * player.
    */
    public SimpleAudioPlayer(File file) throws IOException, NoPlayerException,
    CannotRealizeException {
    this(file.toURL());
    }

    /**
    * Plays the audio file.
    */
    public void play() {
    audioPlayer.start();
    }

    /**
    * Stops and closes the audio file.
    */
    public void stop() {
    audioPlayer.stop();
    audioPlayer.close();
    }

    /**
    * Prints a usage message to System.out for how to use this class
    * through the command line.
    */
    public static void printUsage() {
    System.out.println("Usage: java SimpleAudioPlayer audioFile");
    }

    /**
    * Allows the user to run the class through the command line.
    * Only one argument is allowed, which is the file name of
    * the audio file.
    */
    public static void main(String[] args) {
    try {
    if (args.length == 1) {

    System.out.println("-> Playing file " + args[0] );

    SimpleAudioPlayer player = new SimpleAudioPlayer(args[0]);

    player.play();

    System.out.println(" Press the Enter key to continue.");

    // wait for the user to press Enter to proceed.
    System.in.read();
    System.out.println(" Why doesn't the clip play the second time?");
    player.play();

    System.in.read();
    System.out.println(" .... or the third? ");
    // wait for the user to press Enter to proceed.

    player.play();

    System.out.println("-> Exiting");
    player.stop();
    } else {
    printUsage();
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }

    System.exit(0);
    }
    }
    • Maurie
      Maurie
      2 Posts
      ACCEPTED ANSWER

      Re: Playing a sound file through java program.

      ‏2011-03-20T22:34:39Z  in response to FritzKatz
      there is no need to use JMF to play audio sound from standalone application.
      you can use builtin audio clip for repeated sounds or play directly for streaming sounds.
      email me if you have not found your way through this problem yet. MaurieDanko@gmail.com
    • Maurie
      Maurie
      2 Posts
      ACCEPTED ANSWER

      Re: Playing a sound file through java program.

      ‏2011-03-20T22:34:39Z  in response to FritzKatz
      there is no need to use JMF to play audio sound from standalone application.
      you can use builtin audio clip for repeated sounds or play directly for streaming sounds.
      email me if you have not found your way through this problem yet. MaurieDanko@gmail.com
    • zabalex
      zabalex
      1 Post
      ACCEPTED ANSWER

      Re: Playing a sound file through java program.

      ‏2011-10-15T00:11:19Z  in response to FritzKatz
      Thank you FritzKatz.
      SEO Bay Area