Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private HashMap<String, Integer> initSounds() throws FileNotFoundException {
- HashMap soundList = new HashMap();
- File soundDir = new File("./res/sound/");
- for (int i = 0; i < soundDir.list().length; ++i) {
- if (soundDir.list()[i].endsWith(".wav")) {
- byte[] bits = getBytesFromFile(soundDir.listFiles()[i]);
- WaveData data = WaveData.create(bits);
- if (data != null) {
- int buffer = AL10.alGenBuffers();
- this.bufferList[i] = buffer;
- AL10.alBufferData(buffer, data.format, data.data, data.samplerate);
- data.dispose();
- int source = AL10.alGenSources();
- System.out.println(source);
- this.sourceList[i] = source;
- AL10.alSourcei(source, 4105, buffer);
- soundList.put(soundDir.list()[i], Integer.valueOf(source));
- }
- }
- }
- if (soundList.isEmpty()) {
- // make a new setting for a disabled sound environment.
- }
- return soundList;
- }
- public static byte[] getBytesFromFile(File file) {
- try {
- InputStream is = new FileInputStream(file);
- long length = file.length();
- if (length > Integer.MAX_VALUE) {}
- byte[] bytes = new byte[(int) length];
- int offset = 0;
- int numRead = 0;
- while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
- offset += numRead;
- }
- if (offset < bytes.length) {
- throw new IOException("Could not completely read file " + file.getName());
- }
- is.close();
- return bytes;
- }
- catch (IOException e) {}
- return null;
- }
Advertisement