简体   繁体   English

Java Sound API初始化

[英]Java Sound API Initialization

I use the following code to play back sound in my game. 我使用以下代码在游戏中播放声音。

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;

public class AudioController {

    public static void playback(String fileName) 
        throws LineUnavailableException, UnsupportedAudioFileException, IOException {
        AudioInputStream ais = AudioSystem.getAudioInputStream(AudioController.class.getClassLoader().getResourceAsStream("sounds/"+fileName));

        AudioFormat format = ais.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        Clip clip = (Clip) AudioSystem.getLine(info);

        clip.open(ais);
        clip.start();
    }
}

The problem is: if a sound is played back for the very first time, the application hangs for a short time, as some sort of initialization work would be done. 问题是:如果是第一次播放声音,则应用程序将挂起一小段时间,因为将完成某种初始化工作。 Every sound played after that does not show this behavior. 此后播放的每个声音都不会显示此行为。 How to prevent that? 如何预防呢?

I've run into this issue too, and my experience is that it doesn't have to do with the size of your clip, but rather the fact that lots of classes get loaded the first time you try to start a clip due to the overhead of many JavaSound classes being loaded. 我也遇到过这个问题,我的经验是,它与剪辑的大小无关,而是因为您第一次尝试启动剪辑会加载很多类,这一事实正在加载的许多JavaSound类的开销。

If you add the the argument -verbose:class to java you may see this issue too. 如果将参数-verbose:class添加到java,您可能也会看到此问题。

What works for me is to have a silent.wav file that doesn't have any audio. 对我有用的是拥有一个没有任何音频的silent.wav文件。 I play it up front before the main sound starts and it gets everything primed. 在主声音开始播放之前,我会先播放它,使所有内容都准备好。

First of all, Java's sound library is rather weak compared to something like Java Media Framework, which you might look into, but if this is a simple game with minimal sound effects, Java's sound library should be enough. 首先,与您可能会研究的Java Media Framework之类的东西相比,Java的声音库相当弱,但是如果这是一款具有最小声音效果的简单游戏,那么Java的声音库就足够了。 You may want to give more details on what type of sound files you want to play (format, length, etc) so we can better address your problem. 您可能需要提供有关要播放的声音文件类型(格式,长度等)的更多详细信息,以便我们更好地解决您的问题。

If you want to play short clips of sound (less than a second) you should initialize the Clip at the start, and play it whenever you need it: 如果要播放声音的短片(不到一秒钟),则应在开始时初始化片段,并在需要时播放它:

//initialize this at the beginning of your program
AudioInputStream soundStream = AudioSystem.getAudioInputStream(new File("sound.wav"));

Clip soundEffect = AudioSystem.getClip();
soundEffect.open(soundStream);

...

//later, play the sound
soundEffect.start();

The issue with this is that Clip has a very limited buffer, and you won't be able to play anything remotely long (>1 second on average, in my experience). 问题是Clip的缓冲区非常有限,您无法远程播放任何内容(根据我的经验,平均播放时间> 1秒)。

If you want to play background music, or something longer, and want to avoid managing byte streams yourself, you'll have to look at outside libraries. 如果要播放背景音乐或更长的时间,并且想要避免自己管理字节流,则必须查看外部库。 There exists a BigClip class that I can't seem to find a download link for, but you can have a look: 我似乎找不到BigClip类的下载链接,但您可以看一下:

http://pscode.org/javadoc/org/pscode/xui/sound/bigclip/BigClip.html http://pscode.org/javadoc/org/pscode/xui/sound/bigclip/BigClip.html

When I've had to play longer sound I use my own modified version of the PCMFilePlayer class found here (I add some extra methods to make it work more like a BigClip): 当我不得不播放更长的声音时,我使用我在这里找到的PCMFilePlayer类的修改版本(我添加了一些额外的方法来使其更像BigClip):

http://codeidol.com/java/swing/Audio/Play-Non-Trivial-Audio/ http://codeidol.com/java/swing/Au​​dio/Play-Non-Trivial-Audio/

In either case, I load up as much sound as is reasonable at the very beginning of the program, and play has needed, but in all cases this has been with a very limited number of sounds. 无论哪种情况,我在程序开始时都会加载尽可能多的声音,并且需要播放,但是在所有情况下,声音的数量都非常有限。 If you're playing huge amounts of sound/music or worry about memory usage, you may want to look into libraries like JMF, which are much more powerful. 如果您正在播放大量声音/音乐或担心内存使用情况,则可能需要研究功能更强大的JMF之类的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM