Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  SoundLibrary with Hashtable  (Read 455 times)
0 Members and 1 Guest are viewing this topic.
Offline Raildex

Senior Newbie





« Posted 2013-06-20 10:10:02 »

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?
Offline Danny02
« Reply #1 - Posted 2013-06-20 10:42:36 »

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
Offline Raildex

Senior Newbie





« Reply #2 - Posted 2013-06-20 10:59:39 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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
  }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
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();
    }
  }
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2013-06-20 13:05:10 »

Can it be that your snd file looks like this
1  
2  
"PISTOL";"Sound/pistol.wav"
"FOOT";"Sound/ftstp.wav"


if true, loose the quotation marks
Offline Raildex

Senior Newbie





« Reply #4 - Posted 2013-06-20 13:08:49 »

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

Senior Newbie





« Reply #5 - Posted 2013-06-20 16:35:40 »

Sry for double post Undecided

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?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
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();
    }
  }
}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
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()
  {
  }
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (11 views)
2014-12-28 10:39:27

CopyableCougar4 (17 views)
2014-12-28 02:10:29

BurntPizza (21 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (94 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!