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

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

问题描述

在java中我需要检查是否有音频剪辑运行,但是当我尝试这样的:

 如果(clip1.isRunning()){}

Eclipse中给我的错误:

的方法isRunning()是未定义的类型音频剪辑。

我要补充的东西上的音频剪辑使用isRunning()?还是我做错了什么?

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

 进口java.applet.Applet中;
进口java.applet.AudioClip中;
进口java.awt.Color中;
进口java.awt.Font中;
进口java.awt.Graphics;
进口java.awt.Image中;
进口java.awt.Toolkit中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.awt.event.KeyEvent中;
进口java.awt.event.KeyListener;
进口java.awt.event.MouseEvent中;
进口java.awt.event.MouseListener;
进口了java.util.Random;
javax.sound.sampled.Clip中的进口;
进口javax.swing.Timer中;音频剪辑clip1;公共无效鼠标pressed(我的MouseEvent){
    如果(XPOS大于0&放大器;&放大器; XPOS℃,+ 64&放大器;&放大器; yPos大于0&放大器;&放大器;
            yPos< 0 + 64){
        如果(soundMuted == FALSE){
            soundMuted = TRUE;
            clip1.stop();
        }
        其他{
            如果(clip1.isRunning()){            }
            其他{
                soundMuted = FALSE;
                clip1.play();
            }
        }    }
}

和以下是错误我得到:

 描述资源路径位置类型
该方法isRunning()是未定义的类型音频剪辑HomeScreen.java
/ AlexVega2 / src目录线421 Java问题


解决方案

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

要使用 javax.sound.sampled.Clip中,你就必须利用的声音API ,对于<一个href=\"http://stackoverflow.com/questions/29836255/playing-multiple-sound-clips-using-clip-objects/29836764#29836764\">example和<一个href=\"http://stackoverflow.com/questions/17138614/clip-not-playing-any-sound/17139062#17139062\">example

本例中的音频剪辑预计将嵌入在JAR文件中(这个例子中有他们在声音包,但你可以改变在任何你需要它)

 进口java.awt.GridBagConstraints中;
进口java.awt.GridBagLayout中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口javax.sound.sampled.AudioInputStream中;
进口javax.sound.sampled.AudioSystem;
javax.sound.sampled.Clip中的进口;
进口javax.sound.sampled.LineEvent;
进口javax.sound.sampled.LineListener;
进口javax.sound.sampled.LineUnavailableException;
进口javax.sound.sampled.UnsupportedAudioFileException;
进口javax.swing.JApplet中;
进口javax.swing.JButton中;
进口javax.swing.JLabel中;
进口javax.swing.Timer中;公共类测试扩展JApplet的{    私人夹夹;
    私人的JButton btnPlay;
    私人标签的JLabel;    @覆盖
    公共无效的init(){
        super.init();
    }    @覆盖
    公共无效的start(){
        super.start();
        的setLayout(新的GridBagLayout());
        GridBagConstraints的GBC =新的GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;        btnP​​lay =的新的JButton(带来的噪音);
        标签=新的JLabel(___);        加(btnPlay,GBC);
        添加(标签,GBC);        btnP​​lay.addActionListener(新的ActionListener(){
            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                玩();
            }
        });        定时器定时器=新定时器(250,新的ActionListener(){
            私人INT计数器= 0;
            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                如果(夹== NULL ||!clip.isRunning()){
                    label.setText(____);
                }其他{
                    StringBuilder的SB =新的StringBuilder();
                    对于(INT指数= 0;指数 - LT;计数器;指数++){
                        sb.setCharAt(索引,。);
                    }
                    label.setText(sb.toString());
                    反++;
                    如果(计数器大于10){
                        计数器= 0;
                    }
                }
            }
        });
        timer.setInitialDelay(0);
        timer.start();
    }    保护无效播放(){        尝试(InputStream为=的getClass()。的getResourceAsStream(/听起来与亚麻籽Hair.wav /小龙女)){
            尝试(的AudioInputStream的AudioInputStream = AudioSystem.getAudioInputStream(是)){
                夹= AudioSystem.getClip();
                clip.addLineListener(新LineListener(){
                    @覆盖
                    公共无效更新(LineEvent事件){
                        的System.out.println(event.getFramePosition());
                        如果(event.getType()。等于(LineEvent.Type.STOP)){
                            btnP​​lay.setEnabled(真);
                        }
                    }
                });
                clip.open(的AudioInputStream);
                clip.start();
                btnP​​lay.setEnabled(假);
            }赶上(UnsupportedAudioFileException | LineUnavailableException前){
                ex.printStackTrace();
            }
        }赶上(IOException异常EXP){
            exp.printStackTrace();
        }    }    @覆盖
    公共无效停止(){
        super.stop();
        如果(夹!= 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的如何使用&QUOT; isRunning&QUOT;在音频剪辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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