SoundLibrary with Hashtable

Hey there. I want to make a levelspecific Soundmap with a Hashtable.
Loading the Hashtable is no problem.

The Hashmap(SoundLibrary) is built like this:
Key “PISTOL”
Value “Sound/pistol.wav”

I have a problem with returning the Value of the Key.
The Hashtable returns null with get(“PISTOL”);. But System.out.println(SoundLibrary); prints {“PISTOL” = “Sound/pistol.wav”}
Any Suggestions how to solve this?

  1. pls use HashMap instead of HashTable, which is some legancy code from early Java days.

  2. pls post some small sample code which shows us how you fill and querry the map


package Sound;

import java.util.HashMap;

/**
 *
 * @author Nils
 * Contains a levelspecific Soundmap
 */
public class SoundLibrary
{
  public HashMap<String,String> sounds;
  public SoundLibrary(String levelname)
  {
    sounds = new HashMap<String,String>();
    LevelSoundFileReader lsfr = new LevelSoundFileReader(levelname,this);
    System.out.println(sounds); //prints {"PISTOL"="Sound/pistol.wav", "FOOT"="Sound/ftstp.wav"}
    System.out.println(sounds.get("PISTOL")); //prints null
  }
}



package Sound;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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();
          library.sounds.put(key,file);
        }
        line = br.readLine();
      }
    }
    catch (IOException ex)
    {
      ex.printStackTrace();
    }
  }
}

Can it be that your snd file looks like this


"PISTOL";"Sound/pistol.wav"
"FOOT";"Sound/ftstp.wav"

if true, loose the quotation marks

Yep, that was it.
Makes sense, the file is already a String when loaded into the application, so quotation marks are useless :smiley:

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()
  {
  }
}