我对Java比较陌生,不过现在我正在这里深入研究。
我正在创建一个显示鸡肉按钮的程序,点击此按钮后,会播放鸡咕咕声。看起来简单,但我真的无法让音频工作。
我正在使用AudioClip来尝试实现此目的。我一直用来自学的书,Y.Daniel Liang的[i] Java编程简介[/ i](第8版)告诉我尽管位于非Applet程序中,但我可以使用AudioClip 。
但是,我无法让这个工作,所以这就是我到目前为止所做的。我会给你我的代码,然后告诉你我收到的错误。顺便说一句,我暂时在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,它们都会产生相同的错误。有没有人知道这是怎么回事?
非常感谢任何帮助。
感谢。
编辑: 它现在才有效。不知。