jeudi 25 décembre 2014

Java: sun.sudio: Possible to not load audio file every time it is played?

Is there any way to play the same audio file (Using sun.audio) without having to load the Fileinputstream every time? This is how I am trying to do it right now:



public static void nomnom() throws Exception
{

FileInputStream in = new FileInputStream ("nomnom.au");
AudioStream as = new AudioStream (in);
AudioPlayer.player.start (as);
ContinuousAudioDataStream loop = null;
}


Despite "nomnom.au" being only a few seconds, I quickly get an OutofMemoryError when I repeat this method (to play the sound) about 30 times. I assume that this is caused by the fact that I am loading the audio file every time it needs to be played.


I have tried declaring the FileInputStream and AudioStream at the very beginning of my class, only loading my file once a the beginning of the main method:



public class Snake
{
static InputStream in;
static AudioStream as;
.
.
.
public static void nomnomload () throws Exception
{
in = new FileInputStream ("nomnom.au");
as = new AudioStream (in)
}

public static void nomnomplay() throws Exception
{
AudioPlayer.player.start (as);
ContinuousAudioDataStream loop = null;
}
}


I was hoping to call the nomnomload method only at the beginning of my game. When I try to play the audio, however, I only get sound for the first time, and nothing for my following attempts to run nomnomplay.


Any help to resolve this issue would be greatly appreciated.


Aucun commentaire:

Enregistrer un commentaire