Swing 试图在jframe打开时播放掌声

Swing 试图在jframe打开时播放掌声,swing,jframe,wav,Swing,Jframe,Wav,首先,我会说我打开了Jframe并保持打开状态10秒钟。 我为孩子们写了一些游戏,向他们展示编程或计算机科学的未来。。。反正 无论谁赢了,我都有另一个Jframe设计,当它出现时,我想要一个声音剪辑来播放。目前的声音剪辑是在源文件夹,我有一个小方法来播放它,但它不工作。这是密码 import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.io.IOException; import java.net.

首先,我会说我打开了Jframe并保持打开状态10秒钟。 我为孩子们写了一些游戏,向他们展示编程或计算机科学的未来。。。反正

无论谁赢了,我都有另一个Jframe设计,当它出现时,我想要一个声音剪辑来播放。目前的声音剪辑是在源文件夹,我有一个小方法来播放它,但它不工作。这是密码

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class HoorayWithTimer extends javax.swing.JFrame {

int counter;

public HoorayWithTimer() {
    initComponents();


    Clip click = clipOpen("applause3.wav");// should open the clip
    clipStart(click);


}//end of public HorrayWithTimer()
/*
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    timerLabel = new javax.swing.JLabel();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setPreferredSize(new java.awt.Dimension(571, 410));
    getContentPane().setLayout(null);
    getContentPane().add(timerLabel);
    timerLabel.setBounds(632, 411, 66, 29);
    timerLabel.getAccessibleContext().setAccessibleName("timerLabel");

    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/party_balloons_scaled.png"))); // NOI18N
    getContentPane().add(jLabel1);
    jLabel1.setBounds(0, 0, 560, 540);

    Timer timer = new Timer(10000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       HoorayWithTimer.this.setVisible(false);
       HoorayWithTimer.this.dispose();
    }
    });
    timer.start();


    pack();
}// </editor-fold>

/**
 * @param args the command line arguments
 */
private Clip clipOpen(String name)// method to play clip
{
    URL url = getClass().getResource(name);
    try {
        AudioInputStream stream = AudioSystem.getAudioInputStream(url);
        Clip clip = AudioSystem.getClip();
        clip.open(stream);
        return clip;
    } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
    }

    return null;
}

private void clipStart(Clip clip) {
    clip.start();
}

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>



    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new HoorayWithTimer().setVisible(true);

        }
    });

}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel timerLabel;
// End of variables declaration
}
导入java.awt.event.*;
导入java.awt.*;
导入javax.swing.*;
导入java.io.IOException;
导入java.net.URL;
导入javax.sound.sampled.AudioInputStream;
导入javax.sound.sampled.AudioSystem;
导入javax.sound.sampled.Clip;
导入javax.sound.sampled.LineUnavailableException;
导入javax.sound.sampled.unsupportDaudioFileException;
公共类HoorayWithTimer扩展了javax.swing.JFrame{
整数计数器;
公共HoorayWithTimer(){
初始化组件();
Clip click=clipOpen(“applause3.wav”);//应该打开剪辑
剪贴画(点击);
}//公共HorrayWithTimer()结束
/*
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
// 
私有组件(){
timerLabel=newjavax.swing.JLabel();
jLabel1=newjavax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE\u ON\u CLOSE);
setPreferredSize(新java.awt.Dimension(571,410));
getContentPane().setLayout(null);
getContentPane().add(timerLabel);
时间标签.倒退(632411,66,29);
timerLabel.getAccessibleContext().setAccessibleName(“timerLabel”);
jLabel1.setIcon(新的javax.swing.ImageIcon(getClass().getResource(“/party\u ballos\u scaled.png”));//NOI18N
getContentPane().add(jLabel1);
jLabel1.立根(0,0560540);
计时器计时器=新计时器(10000,新ActionListener(){
已执行的公共无效操作(操作事件e){
HoorayWithTimer.this.setVisible(false);
HoorayWithTimer.this.dispose();
}
});
timer.start();
包装();
}// 
/**
*@param指定命令行参数
*/
private Clip clipOpen(字符串名称)//播放剪辑的方法
{
URL=getClass().getResource(名称);
试一试{
AudioInputStream=AudioSystem.getAudioInputStream(url);
Clip Clip=AudioSystem.getClip();
开(流);
回程夹;
}捕获(不支持DaudioFileException | IOException | LineUnavailableException ex){
}
返回null;
}
专用无效剪辑开始(剪辑剪辑){
clip.start();
}
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.severy,null,ex);
}catch(实例化异常){
getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.severy,null,ex);
}捕获(非法访问例外){
getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.severy,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.severy,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
@凌驾
公开募捐{
新建HoorayWithTimer().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JLabel jLabel1;
私有javax.swing.JLabel timerLabel;
//变量结束声明
}

我认为问题在这里
clipOpen(“applause3.wav”)
。尝试使用
/applause3.wav
。你的wav文件在哪里?例如,它适用于我的路径
“/res/1.mp3”
1.mp3
文件存在于文件夹
src
中的
res
文件夹中

编辑:替换代码并观察异常,它不在
clipStart
方法中:

private Clip clipOpen(String name){
URL url = getClass().getResource(name);
try {
    AudioInputStream stream = AudioSystem.getAudioInputStream(url);
    Clip clip = AudioSystem.getClip();
    clip.open(stream);
    return clip;
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
   ex.printStackTrace()
}

return null;
}

实际上,这是一个学校服务学习项目,wav文件就在src文件夹中。即使使用/i,当我删除Clip click=clipOpen(“/applause3.wav”)时,也会得到一堆空指针异常,这些异常都会消失;剪贴画(点击);您的异常在哪里?我想您还有另一个异常,在
clipOpen
add-in-catch-block
ex.printStackTrace()并在HoorayWithTimer.clipStart(HoorayWithTimer.java:97)的HoorayWithTimer.clipStart(HoorayWithTimer.java:39)的HoorayWithTimer$2.run(HoorayWithTimer.java:133)的java.AWT.event.InvocationEvent.dispatch(InvocationEvent.java:251)中查看线程“AWT-EventQueue-0”中的errorExceptionjava.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)java.awt.EventQueue.access$200(EventQueue.java:103)java.awt.EventQueue$3.run(EventQueue.java:682)java.awt.EventQueue$3.run(EventQueue.java:680)java.security.AccessController.doPrivileged(本机方法)