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 bundled in a resource.

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?

____________________________________

import javax.media.*;

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

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

Found a solution to my own problem. The audio clip needed to be re-wound back to it's beginning in order to play it again.

/**
  * Rewind the audio file.
  */
public void rewind() {
   audioPlayer.setMediaTime(new Time(0));
}

Here's the working code, just add a player.rewind() after each call to player.play():

import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import java.io.IOException;
import javax.media.Time;

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

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

    /**
     * Rewind the audio file.
     */
    public void rewind() {
        audioPlayer.setMediaTime(new Time(0));
    }

    /**
     * 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(" Now clip plays the second time.");
                player.rewind();
                player.play();

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

                player.rewind();
                player.play();

                System.in.read();
                System.out.println("-> Exiting");
                player.stop();

            } else {
                printUsage();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        System.exit(0);
    }
}
This question has already been answered. Start a new discussion instead.