Luis
Luis

Reputation: 35

How to repeat audio in Java?

This is all i have so far. But im not sure if this is the best way to play an audio file, and I also want to know how to repeat an audio file. Any advice?

package ponggame;

import java.applet.Applet;

import java.applet.AudioClip;

public class Sound {

    public static final Sound bgMusic = new Sound("/pong.wav");
    public static final Sound hitPaddle = new Sound("/bounce.wav");

    private AudioClip clip;

    public Sound(String fileName) {
        try {
            // gets the sound file that is passed in the constructor
            clip = Applet.newAudioClip(Sound.class.getResource(fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // plays the music, obviously
    public void play() {
        try {
            new Thread() { //multi-tasking stuff
                public void run(){
                        clip.play();

                }
            }.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 2

Views: 2345

Answers (3)

IHazABone
IHazABone

Reputation: 525

If you just mean play the audio more than once, you could use a while or for loop that calls your play() method. Here is an example of both:

    for(int i = 5; i > 0; i--) {   //For loops work like this: int i = 5 (declare the   
          play();//temporary variable); i > 0 (the condition, so as long as i is 
                //larger than 0, the loop will execute); i-- (what to do after each iteration of the loop).
    }

int j = 5;
while(j > 0) {
      play();
      j--;
}

In each case, the sound should play five times.
AudioClip.loop(); also works.

Upvotes: 0

Jason C
Jason C

Reputation: 40386

im not sure if this is the best way to play an audio

Yup, you're doing it right, presuming this is for an applet. Also, given that you are uncertain of the use of AudioClip, your abstraction of it behind a Sound is a good use of abstraction -- although you do not need to start the audio on a separate thread, unless you find the latency in play start time unacceptable (play() will return immediately and the clip will play asynchronously).

and also want to know how to repeat an audio.

See AudioClip.loop(). It will start looping the audio when you call it, until you call stop().

Upvotes: 2

michaelsnowden
michaelsnowden

Reputation: 6202

Have a look at the code I made to do this:

import java.net.URL;

import javax.sound.sampled.*;
/**
 * This class is designed to take the filename of a .wav 
 * inside of the com.Game package and turn it into a Clip with a playClip() 
 * method
 * @author michaelsnowden
 *
 */
public class Audio 
{
    URL url;
    Clip clip;
    AudioInputStream ais;

    public Audio(String st)
    {   
        clip = null;

        try{
            AudioInputStream audioIn = AudioSystem.getAudioInputStream( getClass().getResource(st));
            clip = AudioSystem.getClip();
            clip.open(audioIn);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    public void playClip()
    {
        if( clip.isRunning() )
        {
            clip.stop();
        }
        clip.setFramePosition( 0 );
        clip.start();
    }
}

It's honestly not that much different than what you're doing, but it's just more efficient, and the methods you want are built in.

Put this class into your package, and then put the .wav files into your package also. Just drag and drop them. Then, in your Applet, just instantiate objects of this class with the name of your .wav file as a string. For example, Audio catMeow = new Audio("loud_meow.wav"). Then, later on, catMeow.playClip();

Upvotes: 1

Related Questions