简单:Java如何使用" isRunning"在一个音频剪辑?

时间:2015-05-26 04:38:16

标签: java eclipse audio error-handling clip

在java中,我需要检查音频片段是否正在运行,但是当我尝试使用以下内容时:

if(clip1.isRunning()){

}

Eclipse给出了错误:

"方法isRunning()未定义为AudioClip类型。"

我是否必须在audioclip上添加要使用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

1 个答案:

答案 0 :(得分:0)

java.applet.AudioClip不会从任何继承自javax.sound.sampled.Clip的类扩展,因此,它没有isRunning方法

要使用javax.sound.sampled.Clip,您必须使用Sound APIexampleexample

此示例的音频剪辑应该嵌入在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系列.,则会为___设置动画,当它不再播放时,它只会是Azure空行