我如何玩一次两个声音? [英] How do I play two sounds at once?

查看:133
本文介绍了我如何玩一次两个声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在一个applet一次打两个声音,这是行不通的。我使用音频剪辑秒。它甚至有可能一次在一个applet打两个声音?

When I try to play two sounds at once in an applet, it won't work. I'm using AudioClips. Is it even possible to play two sounds at once in an applet?

推荐答案

由于Java 1.3+,使用的 剪辑 类的Java API的声音的。它类似于基于applet的音频剪辑类,但更好的。

Since Java 1.3+, use the Clip class of the Java Sound API. It is similar to the applet based AudioClip class, but better.

例如。改编自上 Java的声音信息所示。页

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;

public class LoopSounds {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);

        URL url2 = new URL(
            "http://pscode.org/media/100_2817-linear.wav");
        Clip clip2 = AudioSystem.getClip();
        AudioInputStream ais2 = AudioSystem.
            getAudioInputStream( url2 );
        clip2.open(ais2);

        // loop continuously
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        clip2.loop(Clip.LOOP_CONTINUOUSLY);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // A GUI element to prevent the Clip's daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
    }
}

这篇关于我如何玩一次两个声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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