本文介绍了在JApplet的播放音频剪辑顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图顺序播放音频剪辑,但它们在同一时间全部发挥。我不知道我做错了。能否请你帮忙。我使用的JFrame,这code是给运行时错误。

 音频剪辑点击;
音频剪辑CLICK2;网址urlClick1 = DisplayMath.class.getResource(number11.wav);
单击= Applet.newAudioClip(urlClick1);网址urlClick2 = DisplayMath.class.getResource(number12.wav);
CLICK2 = Applet.newAudioClip(urlClick2);click.play();
click.notify();尝试{
    click2.wait();
}
赶上(InterruptedException的E1){
    // TODO自动生成catch块
    e1.printStackTrace();
}
click2.play();


解决方案

你是想实现的功能是不可能与音频剪辑,但它与剪辑到一个 LineListener 附后。看到这个例子2片段之间翻转。

 进口javax.sound.sampled.LineListener;
进口的javax.swing *。类TwoClips {    公共静态无效的主要(字串[] args)抛出异常{
        网址为url1 =新的URL(HTTP://ps$c$c.org/media/100_2817-linear.wav);
        URL URL2 =新的URL(HTTP://ps$c$c.org/media/leftright.wav);
        最终剪辑clip1 = AudioSystem.getClip();
        clip1.open(AudioSystem.getAudioInputStream(URL1));
        最终剪辑CLIP2 = AudioSystem.getClip();
        clip2.open(AudioSystem.getAudioInputStream(URL2));
        LineListener监听器=新LineListener(){            剪辑currentClip = clip1;            @覆盖
            公共无效更新(LineEvent事件){
                如果(event.getType()== LineEvent.Type.STOP){
                    如果(currentClip == clip1){
                        currentClip = CLIP2;
                    }其他{
                        currentClip = clip1;
                    }
                    currentClip.setFramePosition(0);
                    currentClip.start();
                }
            }
        };
        clip1.addLineListener(监听);
        clip2.addLineListener(监听);
        可运行R =新的Runnable(){            @覆盖
            公共无效的run(){
                clip1.start();
                JOptionPane.showMessageDialog(NULL,关闭我退出!);
            }
        };
        //摇摆的GUI应创建和更新的EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(R);
    }
}

I'm trying to play audio clips sequentially, but they all play at the same time. I'm not sure what I am doing wrong. Could you Please help. I'm using JFrame, and this code is giving runtime error.

AudioClip click;
AudioClip click2;

URL urlClick1 = DisplayMath.class.getResource("number11.wav");
click = Applet.newAudioClip(urlClick1);

URL urlClick2 = DisplayMath.class.getResource("number12.wav");
click2 = Applet.newAudioClip(urlClick2);

click.play();
click.notify();

try {
    click2.wait();
} 
catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
click2.play();
解决方案

The functionality you are wanting to implement is not possible with an AudioClip, but is it with a Clip to which a LineListener is attached. See this example which flips between 2 clips.

import javax.sound.sampled.LineListener;
import javax.swing.*;

class TwoClips {

    public static void main(String[] args) throws Exception {
        URL url1 = new URL("http://pscode.org/media/100_2817-linear.wav");
        URL url2 = new URL("http://pscode.org/media/leftright.wav");
        final Clip clip1 = AudioSystem.getClip();
        clip1.open(AudioSystem.getAudioInputStream(url1));
        final Clip clip2 = AudioSystem.getClip();
        clip2.open(AudioSystem.getAudioInputStream(url2));
        LineListener listener = new LineListener() {

            Clip currentClip = clip1;

            @Override
            public void update(LineEvent event) {
                if (event.getType() == LineEvent.Type.STOP) {
                    if (currentClip == clip1) {
                        currentClip = clip2;
                    } else {
                        currentClip = clip1;
                    }
                    currentClip.setFramePosition(0);
                    currentClip.start();
                }
            }
        };
        clip1.addLineListener(listener);
        clip2.addLineListener(listener);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                clip1.start();
                JOptionPane.showMessageDialog(null, "Close me to exit!");
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

这篇关于在JApplet的播放音频剪辑顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 03:40