Simple: Java how to use “isRunning” on an audio clip?

六眼飞鱼酱① 提交于 2020-01-25 22:54:53

问题


In java i need to check if an audio clip is running but when i try something like:

if(clip1.isRunning()){

}

Eclipse gives me the error of:

"The method isRunning() is undefined for the type AudioClip."

Do i have to add something to use isRunning() on an audioclip? or am i doing something wrong?

Due to it being a long program here is just my imports and me initializing the audioclip and the part where i use it:

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();
            }
        }

    }
}

And here is the error i get:

Description Resource    Path    Location    Type
The method isRunning() is undefined for the type AudioClip  HomeScreen.java                
/AlexVega2/src  line 421    Java Problem

回答1:


java.applet.AudioClip does not extend from any class which inherits from javax.sound.sampled.Clip, therefore, it does not have a isRunning method

To use javax.sound.sampled.Clip, you'll have to make use of the Sound API, for example and example

The audio clip for this example is expected to be embedded within the Jar file (and this example has them in the sounds package, but you can change to where ever you need it)

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();
        }
    }

}

Basically, when playing, this will animate the JLabel with a series if ., when it's no longer playing it will just be a ___ blank line



来源:https://stackoverflow.com/questions/30449573/simple-java-how-to-use-isrunning-on-an-audio-clip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!