简体   繁体   English

Java Clip(声音)“呼应”并且无法有效播放

[英]Java Clip (sound) 'echoing' and not playing efficiantly

SOLVED 解决了

There was a problem with Runnable, so there were acually TWO game threads running at the same time. Runnable出现了问题,因此共有两个游戏线程同时运行。 (Equals problem!) So the second thread that hit the audio player made the error, and thats why it sounded echoy- it was playing from two different threads! (相等的问题!)因此,命中音频播放器的第二个线程出错了,这就是为什么它听起来很响亮的原因-它是从两个不同的线程播放的!

I also am using SourceDataLine, as suggested, to enable longer sound clips. 我还按照建议使用SourceDataLine来启用更长的声音剪辑。

Original post- 原始后


When this code fires, an error pops up saying 该代码触发时,弹出错误提示

IllegalStateException: Mixer is already open

at the clip.open() line. clip.open()行。 I have put in every measure to check if the code is being ran twice, it is not. 我已采取一切措施来检查代码是否运行了两次,不是。 However, even with the error, the sound still plays. 但是,即使出现错误,声音仍然会播放。 On longer clips (30 seconds) it sounds like theres 2 sounds playing, one right after the other. 在较长的剪辑(30秒)上,听起来好像有2种声音在播放,一种紧接着一种。 On REALLY long clips (3 minutes) the sound stutters. 在非常长的剪辑(3分钟)上,声音发st。

( stringFile is a String , such as "example.wav" ) stringFileString ,例如"example.wav"

File soundFile = new File(stringFile);
AudioInputStream inputStream = AudioSystem.getAudioInputStream(soundFile);
AudioFormat format = inputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(inputStream);
clip.start();
playing = true;

If this question was already posted, I am very sorry. 如果这个问题已经发布,我非常抱歉。 I have searched and I found no thread similar to this one. 我已经搜索过,但没有找到与此线程相似的线程。

Just checking, but part of the point of using a Clip is that you can keep the Clip in memory and call it multiple times, without having to reload it from a file. 只是检查,但使用Clip的部分要点是您可以将Clip保留在内存中并多次调用,而不必从文件中重新加载它。

Thus, to start and stop, you would use myClip.open(), myClip.start(), myClip.stop(), and to play it again, myClip.setFramePosition(0) and myClip.start() if I read the Tutorial correctly. 因此,要开始和停止,您可以使用myClip.open(),myClip.start(),myClip.stop(),然后再次播放,如果我读过myClip.setFramePosition(0)和myClip.start(),教程正确。 (I just work with SourceDataLines, usually, so forgive me if I have this wrong.) (通常,我只使用SourceDataLines,因此,如果遇到此错误,请原谅我。)

If you are reloading it from a file every time, could that cause or contribute to the problem? 如果您每次都从文件中重新加载它,这可能会导致该问题或导致该问题吗? In any case, I'd consider switching to a SourceDataLine. 无论如何,我都会考虑切换到SourceDataLine。 Among other things, this eliminates the overhead of having to load the entire clip into memory before it can start playing. 除其他外,这消除了在开始播放之前必须将整个剪辑加载到内存中的开销。

30 seconds is a lot of RAM, by the way. 顺便说一下,30秒是很多RAM。 30 * 44100 = 1.323 MB (and that's with just 1 byte per frame, a more common wav format is 4 times that size). 30 * 44100 = 1.323 MB(每帧只有1个字节,更常见的wav格式是该大小的4倍)。 Thus, with your longer sounds you might be taxing your RAM to the point of page swapping which would cause stuttering. 因此,随着声音的变长,您可能会将RAM负担加重到页面交换点,这将导致卡顿。

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

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