如何使用JButton播放.wav文件?

时间:2014-03-15 23:16:41

标签: java swing audio jbutton embedded-resource

所以最近,我一直试图制作自己的马里奥游戏(对我自己,可能是为了展示我的其他朋友)。游戏包括按钮。当我点击其他游戏中的按钮时,它会发出声音。我很想将这个功能添加到我的游戏中。问题是,它没有发挥作用。我的源代码是:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JButtonClick {

    JButton test = new JButton("Click Me!");
    JPanel panel = new JPanel();

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


    public JButtonClick() {
        JFrame frame = new JFrame("Button-Click test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.add(panel);
        frame.setSize(800, 600);
        frame.setResizable(true);
        frame.setVisible(true);

        panel.add(test);

        test.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
             playSound("JButton.wav");
            }
        });
    }
    public static void main(String[] args) {
        new JButtonClick();
    }
}

我的.wav文件与此类位于同一个包中。但它不是播放我想要的声音,而是说:

java.io.FileNotFoundException: C:\Users\diego\workspace\Super Mario Bros 1\JButton.wav (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at JButtonClick.playSound(JButtonClick.java:21)
at JButtonClick$1.actionPerformed(JButtonClick.java:49)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我做错了什么?!

2 个答案:

答案 0 :(得分:0)

由于放置了.wav文件,因此存在问题。

根据您的问题,您已将其放在与此类相同的包中,这就是您获得FileNotFoundException的原因。

让我向您解释new File(pathname)如何运作:

关于File的Javadoc说:

  

路径名,无论是抽象的还是字符串形式,可以是绝对路径名也可以是相对路径名。绝对路径名是完整的,因为不需要其他信息来定位它表示的文件。相反,相对路径名必须根据从其他路径名获取的信息来解释。默认情况下,java.io包中的类始终根据当前用户目录解析相对路径名。

  • 您正在eclipse中运行此应用程序,然后将.wav文件直接放在项目中(src文件夹外),如下所示:

    AudioSystem.getAudioInputStream(new File("Jbutton.wav"));
    

enter image description here

  • 您也可以尝试将其放在resource文件夹中。

    AudioSystem.getAudioInputStream(new File("resources/Jbutton.wav"));
    

Snapshot 2

答案 1 :(得分:0)

  

自定义声音播放...使用NetBeans ..
    首先导入一些像这样的包或类名

   import java.applet.Applet;
   import java.applet.AudioClip;
   import java.net.URL;

无效,因为在 java.lang.NullPointerException

上生成错误
URL url = getClass().getResource("\\dbmsystem\\src\\sound\\Your_sound_name.wav");

URL url = getClass().getResource("G:\\zala\\dbmsystem\\src\\sound\\Your_sound_name.wav");

但正确的工作就像这样

URL url = getClass().getResource("/sound/Your_sound_name.wav");    
AudioClip clip = Applet.newAudioClip(url);
clip.play();