How to play two sounds at once?

When I try to play two sounds at once in an applet, it doesn't work. I am using AudioClip

s. Can I play two sounds at the same time in an applet?

0


source to share


1 answer


As of Java 1.3+ use Clip

the Java Sound API class. It is similar to AudioClip

an applet based class , but better.

eg. adapted from what is shown in Java Sound info. page .



import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;

public class LoopSounds {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);

        URL url2 = new URL(
            "http://pscode.org/media/100_2817-linear.wav");
        Clip clip2 = AudioSystem.getClip();
        AudioInputStream ais2 = AudioSystem.
            getAudioInputStream( url2 );
        clip2.open(ais2);

        // loop continuously
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        clip2.loop(Clip.LOOP_CONTINUOUSLY);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // A GUI element to prevent the Clip daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
    }
}

      

+2


source







All Articles