如何在按下按钮时循环播放声音并在释放时停止声音?

时间:2014-04-23 15:49:52

标签: java swing audio jbutton keylistener

我试图向此搜索类似的问题,但无法找到。当按钮被释放时,我不知道如何停止声音,而且,我也不知道如何一个接一个地循环声音,当我按住按钮时它再次播放,而前一个循环仍在播放,声音正在成为∞声音的循环。

以下是代码:

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class test {

    String  b[]={"Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M"};
    Action[] actions = new AbstractAction[26];

    public test() throws Exception {

        JFrame frame = new JFrame();

        JButton[] buttons = new JButton[26];

        for(int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton(b[i]);
            buttons[i].setSize(80, 80);
            buttons[i].addKeyListener(new KeyAdapter(){

                public void keyPressed(KeyEvent e){
                        System.out.println(e.getKeyChar());
                        playSound(new File("loopbase/loop1/"+e.getKeyChar()+".wav"));
                }

                public void keyReleased(KeyEvent e){

                }

            });
            frame.add(buttons[i]);
        }

        JPanel contentPane = (JPanel)frame.getContentPane();

        frame.setLayout(new GridLayout(3, 5, 5, 3));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                try {
                    new test();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

    }

    public void playSound(File soundName)
    {
      try 
      {
       AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundName.getAbsoluteFile( ));
       Clip clip = AudioSystem.getClip( );
       clip.open(audioInputStream);
       clip.start( );
      }
      catch(Exception ex)
      {
        System.out.println("Error with playing sound.");
        ex.printStackTrace( );
      }
    }

}

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

  

我不知道按钮释放后如何停止声音?

您可以使用DataLine#stop()停止剪辑。只需保留上次播放剪辑的引用并在线下方调用即可将其停止。

clip.stop();

注意:您可以将其存储在static变量中的某个位置。


示例代码:

private static Clip clip;

...
public void keyReleased(KeyEvent e) {
    if (clip != null) {
        clip.stop();
    }
}
...

public void playSound(File soundName) {        
        ...
        clip = AudioSystem.getClip();            
        ...        
}

答案 1 :(得分:0)

  

当我按住按钮时,它会在前一个循环仍在播放时再次播放。

这是因为您正在创建一个新的keyPress对象File。应该避免这种情况。

停止声音: 我会向boolean方法添加playsound参数。根据传递的参数,我会调用clip.start()clip.stop()(超类DataLine有一个停止方法)。 在playsound(filename,false);中致电keyReleased

public void playSound(File soundName , boolean start)
{

    try 
    {

        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundName.getAbsoluteFile( ));
        Clip clip = AudioSystem.getClip( );
        clip.open(audioInputStream);
        if(start == true)
        clip.start();
        else
        clip.stop();

    }
    catch(Exception ex)
    {

        System.out.println("Error with playing sound.");
        ex.printStackTrace( );

    }

}