0

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
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Intrinza
  • 35
  • 2
  • 8

1 Answers1

0

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

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Could you give me a quick short example how i would change it? because i'm still a little lost on the difference. – Intrinza May 26 '15 at 05:08
  • What, the two linked examples and linked tutorial aren't enough? – MadProgrammer May 26 '15 at 05:18
  • I just need help on how could i get the song inside the "clip" to play. the song name is "Song1" and it's in the same folder.. how would i do that? – Intrinza May 26 '15 at 05:47
  • `getClass().getResource("Song1")` (but shouldn't it have an extension) – MadProgrammer May 26 '15 at 05:48
  • Sorry i'm kinda new to java so i don't know too much.. So can you explain what you mean a bit more. – Intrinza May 26 '15 at 05:53
  • *"So can you explain what you mean a bit more"* which part? Replace `getClass().getResourceAsStream("/sounds/Maid with the Flaxen Hair.wav")` with the `getClass().getResource("Song1")`? – MadProgrammer May 26 '15 at 05:55