Playing Multiple Sounds at once Java

◇◆丶佛笑我妖孽 提交于 2021-01-27 17:37:58

问题


I've been trying to play multiple sounds at once within Java for a little bit now, I have no problem with playing 1 sound at a single time, but if I try to play like 3 at a time, it will only play 1 sound (I hope this makes sense)

Here is my code:

public static void play(String file) {
        try {
            clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(new File(file)));
            clip.start();

            while (!clip.isRunning())
                Thread.sleep(10);
            while (clip.isRunning())
                Thread.sleep(10);

            clip.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

An example of how I use this method:

AudioPlayer.play("sound1.wav");

When I also want to be able to do this

AudioPlayer.play("sound1.wav");
AudioPlayer.play("sound2.wav");
AudioPlayer.play("sound3.wav");

Also, not important but it would also be nice to do something like this as well:

AudioPlayer.getPlayingSound("sound1.wav").setVolume(-0.85f);

回答1:


To play a clip you should follow this code from a java documentation:

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

and since you want to play clips simultaneously, you can try doing this:

  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's daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
    }
    }



回答2:


You might try to use multithreading to play multiple sounds at same time.



来源:https://stackoverflow.com/questions/40153779/playing-multiple-sounds-at-once-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!