Changed sound player, code will work on java 7
This commit is contained in:
+43
-41
@@ -1,13 +1,14 @@
|
|||||||
package audio;
|
package audio;
|
||||||
|
|
||||||
import javax.sound.sampled.AudioFormat;
|
import java.io.FileInputStream;
|
||||||
import javax.sound.sampled.AudioInputStream;
|
|
||||||
import javax.sound.sampled.AudioSystem;
|
|
||||||
import javax.sound.sampled.Clip;
|
|
||||||
|
|
||||||
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() {
|
public AudioPlayer() {
|
||||||
clip = null;
|
clip = null;
|
||||||
@@ -15,13 +16,11 @@ public class AudioPlayer {
|
|||||||
|
|
||||||
public void setClip(Song s) {
|
public void setClip(Song s) {
|
||||||
try {
|
try {
|
||||||
clip = null;
|
if(clip != null){
|
||||||
AudioInputStream ais = s.getAudioStream();
|
clip.close();
|
||||||
AudioFormat baseFormat = ais.getFormat();
|
}
|
||||||
AudioFormat decodeFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
|
fis = new FileInputStream(s.getAudio());
|
||||||
AudioInputStream dais = AudioSystem.getAudioInputStream(decodeFormat, ais);
|
clip = new Player(fis);
|
||||||
clip = AudioSystem.getClip();
|
|
||||||
clip.open(dais);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -30,49 +29,52 @@ public class AudioPlayer {
|
|||||||
public void play() {
|
public void play() {
|
||||||
if (clip == null)
|
if (clip == null)
|
||||||
return;
|
return;
|
||||||
clip.start();
|
new Thread(new Runnable() {
|
||||||
}
|
@Override
|
||||||
|
public void run() {
|
||||||
public void pause()
|
if(clip != null){
|
||||||
{
|
try {
|
||||||
if (clip == null)
|
clip.play();
|
||||||
return;
|
} catch (JavaLayerException e) {
|
||||||
if (clip.isRunning())
|
e.printStackTrace();
|
||||||
{
|
}
|
||||||
clip.stop();
|
}
|
||||||
}
|
}}).start();
|
||||||
}
|
|
||||||
|
|
||||||
public void play(int framePosition) {
|
|
||||||
if (clip == null)
|
|
||||||
return;
|
|
||||||
stop();
|
|
||||||
clip.setFramePosition(framePosition);
|
|
||||||
clip.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
|
||||||
|
public void play(final int framePosition) {
|
||||||
if (clip == null)
|
if (clip == null)
|
||||||
return;
|
return;
|
||||||
if (clip.isRunning())
|
new Thread(new Runnable() {
|
||||||
{
|
@Override
|
||||||
clip.stop();
|
public void run() {
|
||||||
clip.setFramePosition(0);
|
if(clip != null){
|
||||||
}
|
try {
|
||||||
|
clip.play(framePosition);
|
||||||
|
} catch (JavaLayerException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}).start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
stop();
|
|
||||||
if(clip != null)
|
if(clip != null)
|
||||||
{
|
{
|
||||||
clip.close();
|
clip.close();
|
||||||
|
clip = null;
|
||||||
|
fis = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getProgress()
|
public long getProgress()
|
||||||
{
|
{
|
||||||
if(clip != null)
|
if(clip != null)
|
||||||
return clip.getMicrosecondPosition();
|
return clip.getPosition()*1000;
|
||||||
else
|
else
|
||||||
return 0L;
|
return 0L;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ public class Song {
|
|||||||
|
|
||||||
private BufferedImage backgroundImage;
|
private BufferedImage backgroundImage;
|
||||||
private BufferedImage bannerImage;
|
private BufferedImage bannerImage;
|
||||||
private AudioInputStream audioStream;
|
|
||||||
|
|
||||||
private List<SongInstance> songs;
|
private List<SongInstance> songs;
|
||||||
|
|
||||||
@@ -45,7 +44,6 @@ public class Song {
|
|||||||
|
|
||||||
backgroundImage = null;
|
backgroundImage = null;
|
||||||
bannerImage = null;
|
bannerImage = null;
|
||||||
audioStream = null;
|
|
||||||
|
|
||||||
songs = new ArrayList<SongInstance>();
|
songs = new ArrayList<SongInstance>();
|
||||||
}
|
}
|
||||||
@@ -98,27 +96,6 @@ public class Song {
|
|||||||
return audio;
|
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) {
|
public void setAudio(File audio) {
|
||||||
this.audio = audio;
|
this.audio = audio;
|
||||||
}
|
}
|
||||||
@@ -172,12 +149,5 @@ public class Song {
|
|||||||
|
|
||||||
public void close()
|
public void close()
|
||||||
{
|
{
|
||||||
if(audioStream != null)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
audioStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public class GameControl implements JoystickListener, ButtonListener,ActionListe
|
|||||||
this.model = model;
|
this.model = model;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
this.gsm = gsm;
|
this.gsm = gsm;
|
||||||
update = new Timer(0, this);
|
update = new Timer(1000/60, this);
|
||||||
update.start();
|
update.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -58,7 +58,7 @@ public class Window extends JFrame {
|
|||||||
JoystickHandler jsh = new JoystickHandler();
|
JoystickHandler jsh = new JoystickHandler();
|
||||||
|
|
||||||
//Create Instances
|
//Create Instances
|
||||||
SongHandler sh = new SongHandler();
|
final SongHandler sh = new SongHandler();
|
||||||
GameStateManager gsm = new GameStateManager(sh);
|
GameStateManager gsm = new GameStateManager(sh);
|
||||||
GameView view = new GameView(led,gsm);
|
GameView view = new GameView(led,gsm);
|
||||||
GameModel model = new GameModel(sh, gsm);
|
GameModel model = new GameModel(sh, gsm);
|
||||||
|
|||||||
+2
-12
@@ -35,7 +35,7 @@ public class SongHandler {
|
|||||||
if(Window.ON_RASP)
|
if(Window.ON_RASP)
|
||||||
dir = new File(System.getProperty( "user.home" ) + "/ColorStrike/Songs/");
|
dir = new File(System.getProperty( "user.home" ) + "/ColorStrike/Songs/");
|
||||||
else
|
else
|
||||||
dir = new File(System.getProperty( "user.home" ) + "/Documents/songs/");
|
dir = new File(System.getProperty( "user.home" ) + "/Documenten/songs/");
|
||||||
|
|
||||||
songs = DirScanner.scanDirectories(dir);
|
songs = DirScanner.scanDirectories(dir);
|
||||||
System.out.println(songs.size());
|
System.out.println(songs.size());
|
||||||
@@ -76,7 +76,7 @@ public class SongHandler {
|
|||||||
currentSong = songs.get(currentIndex);
|
currentSong = songs.get(currentIndex);
|
||||||
currentSongInstance = currentSong.getSongs().get(0);
|
currentSongInstance = currentSong.getSongs().get(0);
|
||||||
|
|
||||||
p.stop();
|
p.close();
|
||||||
p.setClip(currentSong);
|
p.setClip(currentSong);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,14 +119,4 @@ public class SongHandler {
|
|||||||
p.play((int)currentSong.getSampleStart()*10);
|
p.play((int)currentSong.getSampleStart()*10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pause()
|
|
||||||
{
|
|
||||||
p.pause();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stop()
|
|
||||||
{
|
|
||||||
p.stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ public class PlayState extends GameState {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
sh.stop();
|
|
||||||
sh.play();
|
sh.play();
|
||||||
|
|
||||||
for(int i=1; i<ButtonHandler.getButtons().size(); i++)
|
for(int i=1; i<ButtonHandler.getButtons().size(); i++)
|
||||||
|
|||||||
Reference in New Issue
Block a user