From: Frederick W. <fre...@us...> - 2012-02-19 15:57:32
|
rails/sound/SoundContext.java | 18 +++++++++++------- rails/sound/SoundPlayer.java | 36 +++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 16 deletions(-) New commits: commit 1f16f2ebfa66afd2971dc0c28675d4d360b6d804 Author: Frederick Weld <fre...@gm...> Date: Sun Feb 19 16:56:16 2012 +0100 Fixed bug of initially playing 2 music files for command line loading diff --git a/rails/sound/SoundContext.java b/rails/sound/SoundContext.java index c38b334..f18f190 100644 --- a/rails/sound/SoundContext.java +++ b/rails/sound/SoundContext.java @@ -70,8 +70,10 @@ public class SoundContext { } private void playBackgroundMusic() { - //do nothing if music is not enabled - if (!SoundConfig.isBGMEnabled()) return; + //do nothing if + // - music is not enabled + // - phase is not initialized + if (!SoundConfig.isBGMEnabled() || currentPhase == null) return; String currentRoundConfigKey = null; if (currentRound instanceof StartRound) { @@ -83,7 +85,9 @@ public class SoundContext { } else if (currentRound instanceof EndOfGameRound) { currentRoundConfigKey = SoundConfig.KEY_BGM_EndOfGameRound; } - //only play anything if round is recognized and new music is to be played + //only play anything if + // - round is recognized + // - new music is to be played if (currentRoundConfigKey != null) { String currentPhaseName = ""; if (currentPhase != null) currentPhaseName = currentPhase.getName(); @@ -95,15 +99,15 @@ public class SoundContext { } } } - public void notifyOfPhase(PhaseI newPhase) { - if (!newPhase.equals(currentPhase)) { + synchronized public void notifyOfPhase(PhaseI newPhase) { + if (newPhase != null && !newPhase.equals(currentPhase)) { currentPhase = newPhase; playBackgroundMusic(); } } - public void notifyOfRound(RoundI newRound) { - if (!newRound.equals(currentRound)) { + synchronized public void notifyOfRound(RoundI newRound) { + if (newRound != null && !newRound.equals(currentRound)) { //play stock market opening bell if stock round became current round //and the round before was not commit d6567259d5bee060e02f84393e1db655f8b13342 Author: Frederick Weld <fre...@gm...> Date: Sun Feb 19 16:33:11 2012 +0100 Added caching of sound files (both sfx and background music) diff --git a/rails/sound/SoundPlayer.java b/rails/sound/SoundPlayer.java index be380d3..ecb60fb 100644 --- a/rails/sound/SoundPlayer.java +++ b/rails/sound/SoundPlayer.java @@ -4,7 +4,11 @@ package rails.sound; import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.File; import java.io.FileInputStream; +import java.util.HashMap; +import java.util.Map; import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.Player; @@ -21,6 +25,23 @@ import javazoom.jl.player.advanced.AdvancedPlayer; */ public class SoundPlayer { + private class SoundFileBuffer { + private Map<String,byte[]> fileBuffer = new HashMap<String,byte[]>(); + synchronized public BufferedInputStream getFileInputStream(String fileName) { + if (!fileBuffer.containsKey(fileName)) { + try { + long length = new File(fileName).length(); + FileInputStream fis = new FileInputStream(fileName); + byte[] fileContent = new byte[(int)length]; + fis.read(fileContent); + fileBuffer.put(fileName, fileContent); + } catch (Exception e) {return null;} + } + return new BufferedInputStream( + new ByteArrayInputStream(fileBuffer.get(fileName))); + } + } + private class PlayerThread extends Thread { String fileName; PlayerThread priorThread; @@ -56,9 +77,7 @@ public class SoundPlayer { } public void play() { try { - FileInputStream fis = new FileInputStream(fileName); - BufferedInputStream bis = new BufferedInputStream(fis); - Player player = new Player(bis); + Player player = new Player(soundFileBuffer.getFileInputStream(fileName)); player.play(); player.close(); } @@ -82,9 +101,8 @@ public class SoundPlayer { @Override public void play() { try { - FileInputStream fis = new FileInputStream(fileName); - BufferedInputStream bis = new BufferedInputStream(fis); - PortionPlayer player = new PortionPlayer(bis); + PortionPlayer player = new PortionPlayer( + soundFileBuffer.getFileInputStream(fileName)); player.play(startPos, endPos); player.close(); } @@ -137,9 +155,7 @@ public class SoundPlayer { } while (!isStopped) { - FileInputStream fis = new FileInputStream(fileName); - BufferedInputStream bis = new BufferedInputStream(fis); - player = new Player(bis); + player = new Player(soundFileBuffer.getFileInputStream(fileName)); player.play(); player.close(); } @@ -163,6 +179,8 @@ public class SoundPlayer { private LoopPlayerThread lastBGMThread = null; + private SoundFileBuffer soundFileBuffer = new SoundFileBuffer(); + /** * atomic switching of the pointer to the last thread which played an sfx. * @param newThread Player thread for the new sfx |