_BenNNN 2019-10-13 13:10 采纳率: 0%
浏览 927

为什么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条回答

  • Yong* Qi 2019-10-13 14:48
    关注

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示