如何在Java游戏播放声音?

编程入门 行业动态 更新时间:2024-10-19 02:23:58
本文介绍了如何在Java游戏播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用下面的code成功播放声音在Java(1.5)小程序的游戏:

I'm successfully playing sounds in a Java (1.5) applet game using the following code:

// get an available clip to play it Clip clip = null; for (Clip clipTemp : players) { if (!clipTemp.isOpen()) { clip = clipTemp; break; } } if (clip == null) { // no available player found, don't play return; } clip.open(audioFormat, audioByteData, 0, audioByteData.length); clip.start();

(球员是我在一开始打开,目的是降低延迟的剪辑列表,一行监听器关闭时被检索停止事件就行了。)

(Players are a list of clips that I open at the start with the aim to reduce latency, a line listener closes the line when the stop event is retrieved.)

我现在面临的问题是播放声音时高达1秒间歇延迟。这是pretty差。

The problem I'm facing is intermittent delays of upto 1 second when playing a sound. This is pretty poor.

有什么办法改善吗?是 SourceDataLines 值得考虑的?

Is there any way to improve this? Are SourceDataLines worth considering?

推荐答案

Java小程序是流的素材,只要你想打它,这就是为什么你得到一个延迟的声音文件还没有被加载到内存然而。

The Java Applet is streaming your clip whenever you want to play it, which is why you are getting a delay as the sound file hasn't been loaded into memory yet.

它已经一段时间,因为我做的Java小应用程序编程,但我不记得我曾经pre-负载我所有的剪​​辑,然后后续调用播放不会重新打开文件。

It's been awhile since I have done Java applet programming, but I do remember that I used to pre-load all my clips and then subsequent calls to play would not re-open the files.

下面是从我的旧项目的一个部分code

Here is some code from one of my old projects

Clip shoot; private loadShootWav() { AudioInputStream sample; sample = AudioSystem.getAudioInputStream(this.getClass().getResource("shoot.wav")); shoot = AudioSystem.getClip(); shoot.open(sample); } public void playShootSFX() { shoot.stop(); shoot.setFramePosition(0); shoot.start(); }

更多推荐

如何在Java游戏播放声音?

本文发布于:2023-11-14 15:17:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587774.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声音   如何在   游戏   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!