저는 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 사용에 대한 팁도 환영합니다. 미리 감사드립니다.
게임이 스윙으로 만들어 졌습니까? –
사운드 버퍼링/재생이 별도의 스레드에서 실행되고 있습니까? – WaelJ
예, 게임은 전체 화면에서 jpanel을 contentpane으로 사용하여 스윙을 사용합니다. 프로그래밍에서 사운드가 작동하는 방식에 대해 완전히 확신 할 수는 없지만 별도의 스레드에 있지 않은 것으로 알고 있습니다. 사운드가 재생 될 때 특별히 느슨해 보이지는 않지만 잠시 후 (1 분 정도) 시간이 지나치게 쌓여서 사라지는 것처럼 보입니다. 지금 스레드를 추가하여 도움이되는지 확인해 보겠습니다. – Dancso