Sry for double post :-\
My idea was to read the files, convert them into byte[] and then put them into my Hashtable and playing them whenever I want.
(Because reading Files from the disk is too slow)
I created a new class called “SoundSample”. It’s constructor has the byte[] of the wav-File.
How do I convert my byte[] to a playable,storable sound?
package Sound;
import java.io.*;
import java.util.StringTokenizer;
/**
*
* Reads a specific *.snd-File from the Level and reads its content
* @author Nils
*/
public class LevelSoundFileReader
{
BufferedReader br;
String delims = ";";
StringTokenizer st;
String key = "";
String file = "";
String line;
public LevelSoundFileReader(String levelname, SoundLibrary library)
{
try
{
BufferedReader br = new BufferedReader(new FileReader("Level/" + levelname
+ ".snd"));
line = br.readLine();
while (line != null)
{
st = new StringTokenizer(line, delims);
while (st.hasMoreElements())
{
String key = st.nextToken();
String file = st.nextToken();
//convert the file into byte[]
File soundFile = new File(file);
FileInputStream fis = new FileInputStream(soundFile);
byte[] bytes = new byte[(int)soundFile.length()];
DataInputStream dis = new DataInputStream(fis);
dis.readFully(bytes);
//convert byteArray into a SoundSample
SoundSample sound = new SoundSample(bytes);
//library.put(key,bytes);
}
line = br.readLine();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
package Sound;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.sound.sampled.Control.Type;
import javax.sound.sampled.*;
public class SoundSample
{
Clip soundClip;
/**
* Constructor to create a new SoundSample out of byteArrays
*
* @param data
*/
public SoundSample(byte[] data)
{
try
{
ByteArrayInputStream oInstream = new ByteArrayInputStream(data);
AudioInputStream oAIS = AudioSystem.getAudioInputStream(oInstream);
soundClip.open(oAIS);
}
catch (LineUnavailableException | UnsupportedAudioFileException | IOException ex)
{
}
}
public void play()
{
}
}