作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 Java 开发一款游戏,并尝试编写一个简单的声音系统,它由两个类文件组成:一个用于处理它们,另一个用于加载每个声音文件。
我的问题是,每隔一段时间我就会遇到一个巨大的滞后现象,这意味着完全停止一两秒,真正中断游戏。我怀疑垃圾控制,但我不完全确定。我唯一知道的事实是问题的根源在于声音。
这是我的 SoundHandler.java:
(声音的加载并不重要,据我所知它应该与问题无关)
package Classes;
import java.io.*;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.FloatControl;
public class SoundHandler {
Sound Sounds[] = new Sound[1];
public SoundHandler()
{
if(!LoadSounds("Sounds.cfg"))
System.out.print("Failiure upon loading sounds!");
}
public boolean LoadSounds(String FileName)
{
String line = "";
String SoundName = "";
String SoundFile = "";
String[] token3 = new String[10];
boolean EndOfFile = false;
int LineCount = 0;
BufferedReader characterfile = null;
try
{
characterfile = new BufferedReader(new FileReader(GameMain.GameFolder+"Data/"+FileName));
}
catch(FileNotFoundException fileex)
{
System.out.println(FileName+": file not found.");
return false;
}
while(!EndOfFile && line != null)
{
try
{
line = characterfile.readLine();
if(line != null)
{
if(line.indexOf("//") == -1 && !line.equals(""))
LineCount++;
}
}
catch(IOException ioexception)
{
if(LineCount == 0)
{
System.out.println(FileName+": error loading file.");
return false;
}
EndOfFile = true;
}
}
try { characterfile.close(); } catch(IOException ioexception) { characterfile = null; }
Sounds = new Sound[LineCount];
EndOfFile = false;
LineCount = 0;
try
{
characterfile = new BufferedReader(new FileReader(GameMain.GameFolder+"Data/"+FileName));
}
catch(FileNotFoundException fileex)
{
System.out.println(FileName+": file not found.");
return false;
}
try
{
line = characterfile.readLine();
if(line != null)
if(line.indexOf("//") == -1 && !line.equals(""))
LineCount++;
}
catch(IOException ioexception) { }
while(EndOfFile == false && line != null) {
if(line.indexOf("//") == -1 && !line.equals(""))
{
line = line.trim();
line = line.replaceAll("\t\t", "\t");
line = line.replaceAll("\t\t", "\t");
line = line.replaceAll("\t\t", "\t");
line = line.replaceAll("\t\t", "\t");
int Spot = line.indexOf("\t");
SoundName = line.substring(0,Spot);
SoundFile = line.substring(Spot+1);
Sounds[LineCount-1] = new Sound(SoundName,SoundFile);
}
try {
line = characterfile.readLine();
if(line != null)
if(line.indexOf("//") == -1 && !line.equals(""))
LineCount++;
} catch(IOException ioexception1) { EndOfFile = true; }
}
try { characterfile.close(); } catch(IOException ioexception) { }
return true;
}
public File GetSoundFile(String Name)
{
File result = null;
for(int i = 0; i < Sounds.length; i++)
if(Sounds[i].Name.equals(Name))
result = Sounds[i].File;
return result;
}
public Sound GetSound(String Name)
{
Sound result = null;
for(int i = 0; i < Sounds.length; i++)
if(Sounds[i].Name.equals(Name))
result = Sounds[i];
return result;
}
public int GetSoundID(String Name)
{
int result = 0;
for(int i = 0; i < Sounds.length; i++)
if(Sounds[i].Name.equals(Name))
result = i;
return result;
}
Double MasterVolume = 0.75;
public float CalcVolume(double Vol)
{
float result = 0f;
result = -40.0f + (float)(MasterVolume*Vol*40);
if(result < -40.0f)
result = -40.0f;
if(result > 0.0f)
result = 0.0f;
return result;
}
public boolean PlaySound(String SoundName, double Vol)
{
int ID = GetSoundID(SoundName);
try
{
Clip CurrentClip;
Sounds[ID].Reset(false);
CurrentClip = (Clip) AudioSystem.getLine(Sounds[ID].info);
CurrentClip.addLineListener(new LineListener() {
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
event.getLine().close();
}
}
});
CurrentClip.open(Sounds[ID].sound);
FloatControl Volume;
Volume = (FloatControl) CurrentClip.getControl(FloatControl.Type.MASTER_GAIN);
Volume.setValue(CalcVolume(Vol));
CurrentClip.start();
}
catch(LineUnavailableException e) { e.printStackTrace(); }
catch(IOException e) { e.printStackTrace(); }
return true;
}
}
这是我的 Sound.java:
package Classes;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
public class Sound {
public String Name = "";
public File File = null;
AudioInputStream sound;
DataLine.Info info;
Clip clip;
public Sound(String iName, String FileName)
{
Name = iName;
File = new File(GameMain.GameFolder+"Sound/"+FileName+".wav");
Reset(true);
}
public void Reset(boolean init)
{
try
{
if(!init)
sound.close();
sound = AudioSystem.getAudioInputStream(File);
info = new DataLine.Info(Clip.class, sound.getFormat());
}
catch(IOException e) { e.printStackTrace(); }
catch(UnsupportedAudioFileException e) { e.printStackTrace(); }
}
}
我尝试寻找一种在游戏中实现声音的通用方法,我遇到的大多数方法都没有同时运行同一声音文件的多个实例,这就是我到目前为止所拥有的。我确信存在一些微小的低效率,特别是在加载代码中,但肯定有一些我遗漏的泄漏。也欢迎任何有关自定义 GC 使用的提示。提前致谢。
最佳答案
在 Java Oracle 站点上研究并发线程可能会更容易...我可以获得一个链接。有多种方法可以将线程同步在一起,并且它们确实有示例。
关于java - 为什么我会听到声音中的滞后现象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7295790/
我是一名优秀的程序员,十分优秀!