如何玩JButton的.wav文件? [英] How to Play .wav File with JButton?

查看:208
本文介绍了如何玩JButton的.wav文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以最近,我一直在努力使自己的马里奥游戏(我自己,可能是为了显示我的其他朋友)。游戏包括按钮。当我点击在其他游戏中的一个按钮,它播放声音。我喜欢这个功能添加到我的比赛。问题是,它不玩了。我的源$ C ​​$ c是:

So recently, I have been trying to make my own Mario game (for myself, possibly to show my other friends). Games include buttons. When I click on a button in other games, it plays a sound. I would love to add that feature to my game. The problem is, it doesn't play. My source code is:

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文件是在同一个包为此类。但是,而不是它打我想要的声音,它只是说这样的:

My .wav file is in the same package as this class. But instead of it playing the sound I want, it just says this:

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)

我是什么做错了吗?!

What am I doing wrong?!

推荐答案

下面的问题是由于你的.wav文件的位置。

Here is the problem due to placement of your .wav file.

根据您的问题,您将其放置在同一个包该类这就是为什么你得到 FileNotFoundException异常

As per your question your have placed it in the same package as this class that's why you are getting FileNotFoundException.

让我解释一下你怎么新建文件(路径)

有关的Javadoc的文件说:

Javadoc about File says:

有一个路径,无论是抽象的还是字符串形式,可以是绝对或相对的。一个绝对路径是在没有任何其他信息,以找到它表示文件所需完成。相对路径名,相反,必须在从其他路径采取信息化方面PTED间$ P $。默认情况下,java.io包中的类始终解析相对路径根据当前用户目录。

A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory.


  • 它正在运行在eclipse这个应用,然后直接将.wav文件的项目(外的src 文件夹),如下图所示:

  • It you are running this application in eclipse then place .wav file directly in the project (outside src folder) as shown below:

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



    • 您可以通过将其放置在资源文件夹还尝试。

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


    这篇关于如何玩JButton的.wav文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆