为什么Java播放wav文件没有声音(在application)?

图片说明

大神们,请教下为什么Eclipse运行没有报错,但是点击了“Dou”按钮却没有反应(没有声音),急死我了!!

package b16;

import sun.audio.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.Clip;

import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class b16 extends JFrame implements ActionListener{

    JButton []jb = new JButton[]{new JButton("Dou"),new JButton("Ruai"),new JButton("Mi"),new JButton("Fa"),new JButton("Sou"),new JButton("La"),new JButton("Xi")};
    private Clip clip;

    public b16() throws IOException, UnsupportedAudioFileException, LineUnavailableException{
        super("音阶程序");
        JPanel app = new JPanel(new GridLayout(1, 7));

        Container c = super.getContentPane();
        super.setLocation(400,400);
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);  
        super.setSize(600, 150);
        super.setResizable(false);
        super.setVisible(true);
        c.add(app);

        for(int i = 0;i < 7;i++) {       //添加音阶播放按钮
            app.add(jb[i]);
        }

        InputStream test = new FileInputStream("C:\\Users\\911\\Desktop\\Dou.wav");

        BufferedInputStream test1 = new BufferedInputStream(test);

        AudioInputStream as = AudioSystem.getAudioInputStream(test1);

        clip=AudioSystem.getClip();
        clip.open(as);
    }

    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == jb[0]) {
            clip.start();
            clip.stop();
        }
    }

    public static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException {
            b16 test = new b16();
        }
}

1个回答

InputStream test = new FileInputStream("C:\Users\911\Desktop\Dou.wav");

把音频文件复制到工程下的WebContent下的images文件下;并修改代码:
InputStream test = new FileInputStream("images/Dou.wav");

u012308827
_BenNNN 感谢回答!我后来改了这样就可以了:URL url1 = this.getClass().getClassLoader().getResource("b16/Dou.wav");AudioClip test1 = Applet.newAudioClip(url1);test1.play();
大约一个月之前 回复
u012308827
_BenNNN 感谢回答!我后来改了这样:URL url1 = this.getClass().getClassLoader().getResource("b16/Dou.wav");AudioClip test1 = Applet.newAudioClip(url1);
大约一个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!