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

为什么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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog