首先,我会说我的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
}
答案 0 :(得分:0)
我认为问题在这里clipOpen("applause3.wav")
。尝试使用/applause3.wav
。你的wav文件在哪里?例如,它适用于我的路径"/res/1.mp3"
,1.mp3
文件存在于文件夹res
的{{1}}文件夹中。
编辑:替换您的代码并观看异常,它不在src
方法中:
clipStart