【问题标题】:How to Play .wav File with JButton?如何使用 JButton 播放 .wav 文件?
【发布时间】:2014-03-15 23:16:41
【问题描述】:

所以最近,我一直在尝试制作自己的马里奥游戏(为我自己,可能是给我的其他朋友看)。游戏包括按钮。当我单击其他游戏中的按钮时,它会播放声音。我很想将这个功能添加到我的游戏中。问题是,它不会播放。我的源代码是:

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)

我做错了什么?!

【问题讨论】:

  • 可能是指定了错误的路径?
  • 确实,@JoshM 说的是实话。请理解您正在尝试将 wav 文件作为文件访问,并且默认目录是 user.dir 的目录,而这不是您要查找的位置。如果可能,您应该将 wav 文件作为资源而不是文件来访问。
  • 该资源是embedded-resource。在这种情况下,资源必须由URL 而不是File 访问。请参阅标签的info page,了解形成URL 的方法。
  • 要么使用完整的源路径,要么(至少)从程序的根目录开始。如果您的文件与声音一起在根文件夹中,请使用root/sound.wav
  • FileNotFoundException 通常因为找不到文件而被抛出。

标签: java swing audio jbutton embedded-resource


【解决方案1】:

这是由于您的 .wav 文件放置导致的问题。

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

让我解释一下new File(pathname) 的工作原理:

关于File 的Javadoc 说:

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

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

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

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

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

【讨论】:

    【解决方案2】:

    自定义声音播放...使用 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();
    

    【讨论】:

      猜你喜欢
      • 2011-01-25
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      相关资源
      最近更新 更多