在Java中,我需要检查音频剪辑是否正在运行,但是当我尝试类似的操作时:
if(clip1.isRunning()){
}
Eclipse给了我以下错误:
“类型AudioClip的isRunning()方法未定义。”
我必须添加一些内容才能在音频剪辑上使用isRunning()吗?还是我做错了什么?
由于它是一个很长的程序,所以这里只是我的导入,我要初始化audioclip和使用它的部分:
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.sound.sampled.Clip;
import javax.swing.Timer;
AudioClip clip1;
public void mousePressed(MouseEvent me) {
if (xPos > 0 && xPos < 0+64 && yPos >0 &&
yPos < 0+64){
if(soundMuted == false){
soundMuted = true;
clip1.stop();
}
else{
if (clip1.isRunning()){
}
else{
soundMuted = false;
clip1.play();
}
}
}
}
这是我得到的错误:
Description Resource Path Location Type
The method isRunning() is undefined for the type AudioClip HomeScreen.java
/AlexVega2/src line 421 Java Problem
最佳答案
java.applet.AudioClip
不会从继承自javax.sound.sampled.Clip
的任何类扩展,因此它没有isRunning
方法
要使用javax.sound.sampled.Clip
,您必须使用Sound API,用于example和example
该示例的音频剪辑应嵌入在Jar文件中(此示例将它们包含在sounds
包中,但是您可以更改为所需的任何位置)
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.Timer;
public class Test extends JApplet {
private Clip clip;
private JButton btnPlay;
private JLabel label;
@Override
public void init() {
super.init();
}
@Override
public void start() {
super.start();
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
btnPlay = new JButton("Bring the noise");
label = new JLabel("___");
add(btnPlay, gbc);
add(label, gbc);
btnPlay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play();
}
});
Timer timer = new Timer(250, new ActionListener() {
private int counter = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (clip == null || !clip.isRunning()) {
label.setText("___");
} else {
StringBuilder sb = new StringBuilder(" ");
for (int index = 0; index < counter; index++) {
sb.setCharAt(index, '.');
}
label.setText(sb.toString());
counter++;
if (counter > 10) {
counter = 0;
}
}
}
});
timer.setInitialDelay(0);
timer.start();
}
protected void play() {
try (InputStream is = getClass().getResourceAsStream("/sounds/Maid with the Flaxen Hair.wav")) {
try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is)) {
clip = AudioSystem.getClip();
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
System.out.println(event.getFramePosition());
if (event.getType().equals(LineEvent.Type.STOP)) {
btnPlay.setEnabled(true);
}
}
});
clip.open(audioInputStream);
clip.start();
btnPlay.setEnabled(false);
} catch (UnsupportedAudioFileException | LineUnavailableException ex) {
ex.printStackTrace();
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
@Override
public void stop() {
super.stop();
if (clip != null) {
clip.stop();
}
}
}
基本上,在播放时,如果使用
JLabel
,它将用一系列动画.
,当不再播放时,它将只是___
空行