我对 Java 比较陌生,但现在我在这里更深入地研究了一点。
我正在创建一个显示鸡按钮的程序,单击此按钮后,将播放鸡咯咯声的音频剪辑。看起来很简单,但我真的根本无法让音频正常工作。
我正在使用 AudioClip 来尝试完成此操作。我用来自学的书,Y. Daniel Liang 的 [i]Introduction to Java Programming[/i](第八版)告诉我,尽管 AudioClip 位于非 Applet 程序中,但我仍然可以使用它.
不过,我无法让它发挥作用,所以这就是我到目前为止所拥有的。我会给你我的代码,然后给你我收到的错误。
顺便说一句,我暂时在 Mac 上,还没有机会在 Windows 上试用它。不知道为什么这会影响任何事情,但你去了。
//this is the class that plays the sound. The main class follows.
import javax.swing.JApplet;
import java.applet.*;
import java.net.URL;
public class ChickenSound extends JApplet
{
private AudioClip chSound; //creates AudioClip
public ChickenSound()
{
URL url = getClass().getResource("resources/chicken-sound.wav"); //creates a url that is the path of the soundfile
chSound = this.newAudioClip(url); //sets AudioClip equal to the url
chSound.play(); //plays AudioClip
}
}
//main class
// some of these imports are unnecessary but I will clean them up later
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import java.net.URL;
import javax.swing.*;
public class ChickenButton extends JFrame
{
private ImageIcon chBtnIcon = new ImageIcon("resources/chicken-side.png");
private JButton chBtn = new JButton(chBtnIcon);
private JPanel panel = new JPanel(new GridBagLayout());
public ChickenButton() //main window
{
GridBagConstraints c = new GridBagConstraints();
chBtn.setBorder(BorderFactory.createEmptyBorder());
chBtn.setContentAreaFilled(false);
Font bigFont = new Font("Times New Roman", Font.BOLD, 25);
JLabel chLbl = new JLabel("Click the Chick'n");
chLbl.setFont(bigFont);
c.gridheight = 1;
c.gridx= 0;
c.gridy = 0;
c.weighty = 2;
panel.add(chLbl,c);
c.gridheight=2;
c.gridx= 0;
c.gridy = 1;
c.weighty = 2;
panel.add(chBtn, c);
add(panel);
chBtn.addActionListener(new ChLstnr()); //creates click listener for button
}
public static void main(String[] args)
{
ChickenButton window = new ChickenButton();
window.setLocationRelativeTo(null);
window.setTitle("Chick'n Button");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300, 400);
window.setVisible(true); //creates window
}
class ChLstnr implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Cluck cluck cluck"); //for button functionality testing purposes (and it works)
ChickenSound chSnd = new ChickenSound(); //this is supposed to play the sound. This doesn't work.
}
}
}
我收到的错误是......
2012-07-06 15:44:33.365 java[41264:ff03] Error loading /Library/Audio/Plug-Ins/HAL/JackRouter.plugin/Contents/MacOS/JackRouter: dlopen(/Library/Audio/Plug-Ins/HAL/JackRouter.plugin/Contents/MacOS/JackRouter, 262): no suitable image found. Did find:/Library/Audio/Plug-Ins/HAL/JackRouter.plugin/Contents/MacOS/JackRouter: no matching architecture in universal wrapper
2012-07-06 15:44:33.367 java[41264:ff03] Cannot find function pointer New_JackRouterPlugIn for factory 7CB18864-927D-48B5-904C-CCFBCFBC7ADD in CFBundle/CFPlugIn 0x102191490 </Library/Audio/Plug-Ins/HAL/JackRouter.plugin> (bundle, not loaded)
我真的不知道这意味着什么。显然是和一个无法加载的音频插件有关,但我真的不知道。从看起来这可能是我的计算机出现的错误,而不是我弄乱 Java 的问题,但到目前为止我还没有找到解决方案。我已经用两个 IDE、Eclipse 和 Netbeans 进行了尝试,它们都产生了相同的错误。有谁知道这是怎么回事?
任何帮助是极大的赞赏。
谢谢。
编辑:它现在才有效。不知何故。