简单:Java如何使用“isRunning”在音频剪辑? [英] Simple: Java how to use "isRunning" on an audio clip?

查看:311
本文介绍了简单:Java如何使用“isRunning”在音频剪辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if(clip1。 isRunning()){

}

Eclipse给了我错误:



方法isRunning()是AudioClip类型的未定义。



我必须添加一些东西在音频剪辑上使用isRunning()?或者我做错了什么?



由于这是一个长的程序,这里只是我的进口,我初始化音频剪辑和我使用它的部分:

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

}
}

这里是我得到的错误:

 描述资源路径位置类型
方法isRunning()是未定义类型AudioClip HomeScreen.java
/ AlexVega2 / src行421 Java问题


解决方案

java.applet.AudioClip 不会从继承自 javax.sound.sampled.Clip 因此,它没有 isRunning 方法



要使用 javax.sound.sampled.Clip ,你必须使用声音API 示例示例



此示例的音频剪辑预计将嵌入到Jar文件中(此示例包含它们在声音包中,但您可以更改到您需要的位置)

  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;
私人JButton btnPlay;
私人JLabel标签;

@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(带来噪音);
label = new JLabel(___);

add(btnPlay,gbc);
add(label,gbc);

btnPlay.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
play();
}
});

定时器定时器=新的定时器(250,新的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 作为动画,当不再播放时,它将只是一个 ___ 空白行


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

解决方案

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

这篇关于简单:Java如何使用“isRunning”在音频剪辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆