From ab5fd4007d963aebd2db08e2cf2aab59bb2314be Mon Sep 17 00:00:00 2001 From: jancoow Date: Fri, 5 Jun 2015 20:45:39 +0200 Subject: [PATCH] Changed sound player, code will work on java 7 --- audio/AudioPlayer.java | 84 +++++++++++++++++----------------- audio/Song.java | 30 ------------ control/GameControl.java | 2 +- main/Window.java | 2 +- model/SongHandler.java | 14 +----- model/gameState/PlayState.java | 1 - 6 files changed, 47 insertions(+), 86 deletions(-) diff --git a/audio/AudioPlayer.java b/audio/AudioPlayer.java index c05b05f..ec89b69 100644 --- a/audio/AudioPlayer.java +++ b/audio/AudioPlayer.java @@ -1,13 +1,14 @@ package audio; -import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.Clip; +import java.io.FileInputStream; -public class AudioPlayer { +import javazoom.jl.decoder.JavaLayerException; +import javazoom.jl.player.Player; - private Clip clip; +public class AudioPlayer{ + + private Player clip; + private FileInputStream fis; public AudioPlayer() { clip = null; @@ -15,13 +16,11 @@ public class AudioPlayer { public void setClip(Song s) { try { - clip = null; - AudioInputStream ais = s.getAudioStream(); - AudioFormat baseFormat = ais.getFormat(); - AudioFormat decodeFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false); - AudioInputStream dais = AudioSystem.getAudioInputStream(decodeFormat, ais); - clip = AudioSystem.getClip(); - clip.open(dais); + if(clip != null){ + clip.close(); + } + fis = new FileInputStream(s.getAudio()); + clip = new Player(fis); } catch (Exception e) { e.printStackTrace(); } @@ -30,49 +29,52 @@ public class AudioPlayer { public void play() { if (clip == null) return; - clip.start(); - } - - public void pause() - { - if (clip == null) - return; - if (clip.isRunning()) - { - clip.stop(); - } - } - - public void play(int framePosition) { - if (clip == null) - return; - stop(); - clip.setFramePosition(framePosition); - clip.start(); + new Thread(new Runnable() { + @Override + public void run() { + if(clip != null){ + try { + clip.play(); + } catch (JavaLayerException e) { + e.printStackTrace(); + } + } + }}).start(); + } - public void stop() { + + public void play(final int framePosition) { if (clip == null) - return; - if (clip.isRunning()) - { - clip.stop(); - clip.setFramePosition(0); - } + return; + new Thread(new Runnable() { + @Override + public void run() { + if(clip != null){ + try { + clip.play(framePosition); + } catch (JavaLayerException e) { + e.printStackTrace(); + } + } + }}).start(); + } + public void close() { - stop(); if(clip != null) { clip.close(); + clip = null; + fis = null; } } public long getProgress() { if(clip != null) - return clip.getMicrosecondPosition(); + return clip.getPosition()*1000; else return 0L; } diff --git a/audio/Song.java b/audio/Song.java index a955a83..f98609c 100644 --- a/audio/Song.java +++ b/audio/Song.java @@ -27,7 +27,6 @@ public class Song { private BufferedImage backgroundImage; private BufferedImage bannerImage; - private AudioInputStream audioStream; private List songs; @@ -45,7 +44,6 @@ public class Song { backgroundImage = null; bannerImage = null; - audioStream = null; songs = new ArrayList(); } @@ -98,27 +96,6 @@ public class Song { return audio; } - public AudioInputStream getAudioStream() - { - if(audioStream == null) - { - try { - this.audioStream = AudioSystem.getAudioInputStream(audio); - } catch (UnsupportedAudioFileException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - try { - audioStream.reset(); - } catch (IOException e) { - e.printStackTrace(); - } - return audioStream; - } - public void setAudio(File audio) { this.audio = audio; } @@ -172,12 +149,5 @@ public class Song { public void close() { - if(audioStream != null) - { - try { - audioStream.close(); - } catch (IOException e) { - } - } } } diff --git a/control/GameControl.java b/control/GameControl.java index d018dcf..86c6c60 100644 --- a/control/GameControl.java +++ b/control/GameControl.java @@ -25,7 +25,7 @@ public class GameControl implements JoystickListener, ButtonListener,ActionListe this.model = model; this.view = view; this.gsm = gsm; - update = new Timer(0, this); + update = new Timer(1000/60, this); update.start(); } diff --git a/main/Window.java b/main/Window.java index d9db207..b1c6d7f 100644 --- a/main/Window.java +++ b/main/Window.java @@ -58,7 +58,7 @@ public class Window extends JFrame { JoystickHandler jsh = new JoystickHandler(); //Create Instances - SongHandler sh = new SongHandler(); + final SongHandler sh = new SongHandler(); GameStateManager gsm = new GameStateManager(sh); GameView view = new GameView(led,gsm); GameModel model = new GameModel(sh, gsm); diff --git a/model/SongHandler.java b/model/SongHandler.java index 2995cd3..a1e8f9a 100644 --- a/model/SongHandler.java +++ b/model/SongHandler.java @@ -35,7 +35,7 @@ public class SongHandler { if(Window.ON_RASP) dir = new File(System.getProperty( "user.home" ) + "/ColorStrike/Songs/"); else - dir = new File(System.getProperty( "user.home" ) + "/Documents/songs/"); + dir = new File(System.getProperty( "user.home" ) + "/Documenten/songs/"); songs = DirScanner.scanDirectories(dir); System.out.println(songs.size()); @@ -76,7 +76,7 @@ public class SongHandler { currentSong = songs.get(currentIndex); currentSongInstance = currentSong.getSongs().get(0); - p.stop(); + p.close(); p.setClip(currentSong); } @@ -119,14 +119,4 @@ public class SongHandler { p.play((int)currentSong.getSampleStart()*10); } } - - public void pause() - { - p.pause(); - } - - public void stop() - { - p.stop(); - } } diff --git a/model/gameState/PlayState.java b/model/gameState/PlayState.java index 0922d73..ed022ce 100644 --- a/model/gameState/PlayState.java +++ b/model/gameState/PlayState.java @@ -51,7 +51,6 @@ public class PlayState extends GameState { @Override public void init() { - sh.stop(); sh.play(); for(int i=1; i