【问题标题】:Simple: Java how to use "isRunning" on an audio clip?简单:Java 如何在音频剪辑上使用“isRunning”?
【发布时间】:2015-05-26 13:19:52
【问题描述】:

在 java 中,我需要检查音频剪辑是否正在运行,但是当我尝试以下操作时:

if(clip1.isRunning()){

}

Eclipse 给我的错误是:

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

我必须添加一些东西才能在音频剪辑上使用 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();
            }
        }

    }
}

这是我得到的错误:

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

【问题讨论】:

  • 你导入javax.sound.sampled.Clip了吗?
  • 考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将减少混乱并获得更好的响应
  • 可以分享一点点代码吗?
  • 是的,我刚刚导入它,我仍然得到同样的错误。
  • java.applet.AudioClip 没有继承自javax.sound.sampled.Clip 的任何类,因此它没有isRunning 方法

标签: java eclipse audio error-handling clip


【解决方案1】:

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 设置动画,当它不再播放时,它将只是一个___ 空白行

【讨论】:

  • 你能给我一个简短的例子,我将如何改变它?因为我仍然有点迷失在差异上。
  • 什么,两个链接的例子和链接的教程还不够?
  • 我只需要有关如何让“剪辑”中的歌曲播放的帮助。歌曲名称是“Song1”,它在同一个文件夹中。我该怎么做?
  • getClass().getResource("Song1")(但不应该有扩展名)
  • 对不起,我对java有点陌生,所以我不太了解。所以你能解释一下你的意思吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多