爪哇 - 一次同样的声音多次打 [英] Java - playing the same sound multiple times at once

查看:134
本文介绍了爪哇 - 一次同样的声音多次打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个游戏,我有一些问题玩同样的声音多次,因为如果你激活已经打不应该取消第一次一个声音英寸我发现这里#2的解决方案是将其读入一个字节数组,我做了以下方式:

I'm developing a game and I'm having some issues playing the same sound multiple times, as in if you activate one sound that is already playing it shouldn't cancel the first one. The solution I found here on Stackoverflow was to read it into a byte array which I did the following way:

public SoundObject(AudioInputStream audioIn) {

    try {
        af = audioIn.getFormat();
        size = (int) (af.getFrameSize() * audioIn.getFrameLength());
        audio = new byte[size];
        info = new DataLine.Info(Clip.class, af, size);
        audioIn.read(audio, 0, size);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void playSound() {
    try {
        Clip localSound = (Clip) AudioSystem.getLine(info);
        localSound.open(af, audio, 0, size);
        localSound.start();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    }

}

这完美地工作到现在为止,我试图将其导出一个jar文件,由于某种原因的jar文件播放的字节数组导致声音只有几毫秒后切断的问题,我设法找到一个用后如果有人提出以下解决同一个问题:

This worked perfectly up until now that I tried to export it in a jar file, the jar file for some reason has issues playing a byte array which leads to the sounds cutting off after just a few milliseconds, I managed to find one post with the same issue where someone suggested the following solution:

    public SoundObject(String filePath) {
    try {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Loader.class.getResource(filePath));
        clip = AudioSystem.getClip();
        clip.open(audioInputStream);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    }
}

// Play the sound in a separate thread.
public void playSound() {
    Runnable soundPlayer = new Runnable() {

        @Override
        public void run() {
            try {
                clip.setMicrosecondPosition(0);
                clip.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    new Thread(soundPlayer).start();
}

使用第二种方法工作jar文件但随后的问题是而不是声音没有发挥好,因为它像以前,我有一个拍摄功能,如果你拍摄非常快的这种方法并不总是拿起有时不能玩的时候我preSS空间来拍摄声音。因此,与这些解决方案2我从不是有可靠的声音,并不总是扮演否则我将无法将其导出到一个可运行的JAR。

Using the second method worked for jar files but the issue then is instead that the sound does not play as good as it did before, I have a shoot function and if you shoot very fast this method doesn't always pick up that and sometimes fails to play a sound when I press space to shoot. So with these 2 solutions I have to choose from either having unreliable sound that doesn't always play or I won't be able to export it to a runnable JAR.

有没有人有这类问题的经验?

Does anyone have any experience with these kind of issues?

编辑:这里有一个片段,显示你的两瓶是如何工作的:的 https://www.youtube.com/watch?v=wZPOIhHZSJM&

edit: Here's a clip to show you how the 2 jars work: https://www.youtube.com/watch?v=wZPOIhHZSJM&

推荐答案

我的建议是尝试TinySound库。它是免费的code,在GitHub上可用。这code提供了一个最小混频器和剪辑可以同时播放。

What I would suggest is trying the TinySound library. It is free code, available on github. This code provides a minimal mixer, and Clips can be played concurrently.

我有一个很难搞清楚什么可能是走错了你的code。我也做了,我在游戏中使用混频器,它支持并发剪辑播放。我的声音存储为-1之间的花车1。每个玩实例从一个pre-发游泳池光标(这限制并发播放次数),跟踪浮标阵列中的位置,并谘询及增加在while循环,其中各种声音加到一起(简单的加法),并转换成可经由SourceDataLine的输出字节。这是基本的计划,如果你决定走这条路。

I am having a hard time figuring out what might be going wrong with your code. I also made a mixer that I use in games, and it supports concurrent playbacks for Clips. I store the sounds as floats between -1 to 1. Each play instance takes a cursor from a pre-made pool (which limits the number of concurrent playbacks) that tracks the position in the float array, and is consulted and incremented in the while loop where the various sounds are added together (simple addition) and converted to bytes that are output via a SourceDataLine. That's the basic plan, if you decide to go that route.

您可以通过一个线程池,并让您的声音尽可能接近要准备在被称为打得到你的​​第二个设置一些更多的效益。用搅拌机,你只需要一个线程用于音频。

You might get some more efficiency in your second setup by making a thread pool, and having your sounds as close as possible to ready to play upon being called. With a mixer, you only need one thread for the audio.

有在第一实施例的任何类型的错误?我不明白,从给定code,为什么声音会得到切断,由于使用了罐子。被切割的东西他们,如另一种声音,或者调用线程的结局?

Is there any sort of error in the first example? I don't see, from the code given, why sounds would get cut off due to the use of a jar. Is something cutting them off, such as another sound, or an ending of the calling thread?

这篇关于爪哇 - 一次同样的声音多次打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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