简体   繁体   English

如何一次播放一个.wav文件?

[英]How to play .wav files one at a time?

I want to create a program that plays one certain .wav file when a certain button is pressed. 我想创建一个程序,当按下某个按钮时播放一个特定的.wav文件。 For example, JButton b1, b2, b3 plays "Music1.wav" , "Music2.wav" , and "Music3.wav" respectively, but only one wav file can only be played at a time. 例如, JButton b1, b2, b3播放"Music1.wav""Music2.wav""Music3.wav" ,但一次只能播放一个wav文件。 This is what I did, which has problems with stopping the earlier clip and causing two or more wav files to play simultaneously. 这是我所做的,在停止较早的剪辑并导致同时播放两个或多个wav文件时遇到问题。

...   //imports the important stuff 
public class WAVButtons extends JFrame implements ActionListener
{
    private JButtons b1, b2, b3;
    private File[] sounds;

    public WAVButtons()
    {
         try
         {
               sounds = new File[]
                        {new File("Music1.wav"),
                         new File("Music2.wav"),
                         new File("Music3.wav")};
         }catch(Exception e)
         {
         }
         b1 = new JButton("play Music1");
         b2 = new JButton("play Music2");
         b3 = new JButton("play Music3");
         b1.addActionListener(this);
         b2.addActionListener(this);
         b3.addActionListener(this);
         ... //codes for GUI
    }

    public void actionPerformed(ActionEvent ae)
    {
         if(ae.getSource() == b1)
         { 
              playMusic1(true);
              playMusic2(false);
              playMusic3(false);
         }
         else if(ae.getSource() == b2)
         {
              playMusic1(false);
              playMusic2(true);
              playMusic3(false);
         }
         else if(ae.getSource() == b3)
         {
              playMusic1(false);
              playMusic2(false);
              playMusic3(true);
         }
    }

    public void playMusic1(boolean action)
    {
        try
        {
            AudioInputStream stream = AudioSystem.getAudioInputStream(sounds[0]);
            AudioFormat format = stream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            Clip clip = (Clip)AudioSystem.getLine(info);
            clip.open(stream);
            if(action)
            {
                clip.start();
                clip.loop(Clip.LOOP_CONTINUOUSLY);
            }
            else
                clip.stop();
        }catch (Exception e)
        {
        }
    }

    ... //playMusic2() and playMusic3() looks exactly the same as playMusic1(), except that the
        //parameter of AudioSystem.getAudioInputStream() is sounds[1] and sounds[2], respectively.
}

When you call playMusic1(true) followed by playMusic1(false), the second call is going to create a new instance of a clip and then stop the new instance. 当您依次调用playMusic1(true)和playMusic1(false)时,第二个调用将创建一个剪辑的新实例,然后停止该新实例。 The first instance created by the first call is going to keep playing along. 由第一个调用创建的第一个实例将继续播放。 You should hang on to a reference to the clip so that it can be stopped. 您应该坚持对剪辑的引用,以便可以将其停止。 I've also taken the liberty of removing your duplicate code. 我还自由删除了重复的代码。

Clip clip;

public void actionPerformed(ActionEvent ae)
{
     if(ae.getSource() == b1)
     { 
          playMusic(0);
     }
     else if(ae.getSource() == b2)
     {
          playMusic(1);
     }
     else if(ae.getSource() == b3)
     {
          playMusic(2);
     }
}

public void playMusic(int clipNum)
{
    try
    {
        if (clip != null)
            clip.stop();

        if (action)
        { 
            AudioInputStream stream = AudioSystem.getAudioInputStream(sounds[clipNum]);
            AudioFormat format = stream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            clip = (Clip)AudioSystem.getLine(info);
            clip.open(stream);
            clip.start();
            clip.loop(Clip.LOOP_CONTINUOUSLY);
        }
    }catch (Exception e)
    {
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM