您好我试图制作一个java鼓组件,但是当我点击按钮时我的代码没有播放声音,我不知道为什么。我有文件中的声音文件与代码,所以我知道这不是问题。这是我的代码......
import java.applet.AudioClip;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
public class FinalProjectst extends JApplet implements ActionListener {
private JButton snareButton;
private JButton hiHatButton;
private JButton bassButton;
private JButton cymbalsButton;
private AudioClip snare;
private AudioClip hiHat;
private AudioClip bass;
private AudioClip cymbals;
public void init() {
setLayout(new FlowLayout());
sampleButtons();
snare = getAudioClip(getCodeBase(), "Snare.wav");
hiHat = getAudioClip(getCodeBase(), "HiHat.wav");
bass = getAudioClip(getCodeBase(), "Kick.wav");
cymbals = getAudioClip(getCodeBase(), "Crash.wav");
}
private void sampleButtons() {
snareButton = new JButton("Snare");
hiHatButton = new JButton("Hi Hat");
bassButton = new JButton("Kick");
cymbalsButton = new JButton("Cymbals");
snareButton.addActionListener(new ButtonListener());
hiHatButton.addActionListener(new ButtonListener());
bassButton.addActionListener(new ButtonListener());
cymbalsButton.addActionListener(new ButtonListener());
add(snareButton);
add(hiHatButton);
add(bassButton);
add(cymbalsButton);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == snareButton)
snare.play();
if (e.getSource() == hiHatButton)
hiHat.play();
if (e.getSource() == bassButton)
bass.play();
if (e.getSource() == cymbalsButton)
cymbals.play();
}
}
答案 0 :(得分:1)
本代码有很多不好的东西,但总之我从getCodeBase()返回“file:/ tmp /”。所以你指的是* .wav文件为空。
使用此代码,第一个按钮将在网络上播放一段有趣的WAV声音。
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.MalformedURLException;
import java.net.URL;
public class FinalProjectst extends JApplet implements ActionListener
{
private JButton snareButton;
private JButton hiHatButton;
private JButton bassButton;
private JButton cymbalsButton;
private AudioClip snare;
private AudioClip hiHat;
private AudioClip bass;
private AudioClip cymbals;
public void init()
{
setLayout (new FlowLayout());
sampleButtons();
try {
snare = getAudioClip(new URL("http://download.wavetlan.com/SVV/Media/HTTP/WAV/Media-Convert/Media-Convert_test1_Alaw_Mono_VBR_8SS_16000Hz.wav") ); //getCodeBase(), "Snare.wav");
} catch (MalformedURLException e) {
e.printStackTrace();
}
hiHat = getAudioClip(getCodeBase(), "HiHat.wav");
bass = getAudioClip(getCodeBase(), "Kick.wav");
cymbals = getAudioClip(getCodeBase(), "Crash.wav");
}
private void sampleButtons()
{
snareButton = new JButton("Snare");
hiHatButton = new JButton("Hi Hat");
bassButton = new JButton("Kick");
cymbalsButton = new JButton("Cymbals");
snareButton.addActionListener(this); //new ButtonListener()
hiHatButton.addActionListener(this);
bassButton.addActionListener(this);
cymbalsButton.addActionListener(this);
add(snareButton);
add(hiHatButton);
add(bassButton);
add(cymbalsButton);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == snareButton) {
snare.play();
System.out.println(getCodeBase());
}
if (e.getSource() == hiHatButton)
hiHat.play();
if (e.getSource() == bassButton)
bass.play();
if (e.getSource() == cymbalsButton)
cymbals.play();
}
}
这个工作的唯一原因是因为我将缺少的ButtonListener()更改为“this”(如果你有类,则不需要)。现在,您有了第一个按钮的现有URL的工作路径。它播放声音很好。
我建议您通过getCodeBase()或其他方法寻找获得所需网址的方法。