Эх сурвалжийг харах

Changed sound player, code will work on java 7

jancoow 10 жил өмнө
parent
commit
ab5fd4007d

+ 42 - 40
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();
-		}
+		new Thread(new Runnable() {
+			@Override
+			public void run() {
+				if(clip != null){
+					try {
+						clip.play();
+					} catch (JavaLayerException e) {
+						e.printStackTrace();
+					}
+				}
+			}}).start();
+		
 	}
+
 	
-	public void play(int framePosition) {
+	public void play(final int framePosition) {
 		if (clip == null)
-			return;
-		stop();
-		clip.setFramePosition(framePosition);
-		clip.start();
-	}
+			return;	
+		new Thread(new Runnable() {
+			@Override
+			public void run() {
+				if(clip != null){
+					try {
+						clip.play(framePosition);
+					} catch (JavaLayerException e) {
+						e.printStackTrace();
+					}
+				}
+			}}).start();
 
-	public void stop() {
-		if (clip == null)
-			return;
-		if (clip.isRunning())
-		{
-			clip.stop();
-			clip.setFramePosition(0);
-		}
 	}
 
+
 	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;
 	}

+ 0 - 30
audio/Song.java

@@ -27,7 +27,6 @@ public class Song {
 	
 	private BufferedImage backgroundImage;
 	private BufferedImage bannerImage;
-	private AudioInputStream audioStream;
 
 	private List<SongInstance> songs;
 
@@ -45,7 +44,6 @@ public class Song {
 		
 		backgroundImage = null;
 		bannerImage = null;
-		audioStream = null;
 
 		songs = new ArrayList<SongInstance>();
 	}
@@ -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) {
-			}
-		}
 	}
 }

+ 1 - 1
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();
 	}
 

+ 1 - 1
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);

+ 2 - 12
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();
-	}
 }

+ 0 - 1
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<ButtonHandler.getButtons().size(); i++)