prepare for jmp3
This commit is contained in:
@@ -1,112 +0,0 @@
|
||||
package sjgs.sound;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import javazoom.jl.decoder.JavaLayerException;
|
||||
import javazoom.jl.player.FactoryRegistry;
|
||||
import javazoom.jl.player.JavaSoundAudioDevice;
|
||||
import javazoom.jl.player.advanced.AdvancedPlayer;
|
||||
import javazoom.jl.player.advanced.PlaybackListener;
|
||||
|
||||
// NOTE: This class needs the JLayer.jar in your main project in order to function!
|
||||
|
||||
public class MusicPlayer extends PlaybackListener {
|
||||
|
||||
private AdvancedPlayer player;
|
||||
private SoundThread thread;
|
||||
private JavaSoundAudioDevice device;
|
||||
private InputStream stream;
|
||||
private String filename;
|
||||
private final float gain;
|
||||
private final boolean loop;
|
||||
|
||||
private boolean paused;
|
||||
|
||||
public MusicPlayer(final String filename, final float gain, final boolean loop) {
|
||||
this.filename = filename; this.gain = gain; this.loop = loop;
|
||||
createAdvancedPlayer();
|
||||
}
|
||||
public MusicPlayer(final String filename, final boolean loop) {
|
||||
this.filename = filename; gain = 0; this.loop = loop;
|
||||
createAdvancedPlayer();
|
||||
}
|
||||
|
||||
private void createAdvancedPlayer() {
|
||||
try {
|
||||
final JavaSoundAudioDevice device = (JavaSoundAudioDevice) FactoryRegistry.systemRegistry().createAudioDevice();
|
||||
device.createSource();
|
||||
device.setGain(gain);
|
||||
stream = SoundPlayer.class.getResourceAsStream(filename);
|
||||
player = new AdvancedPlayer(stream, device);
|
||||
} catch (final Exception e) { e.printStackTrace(); }
|
||||
player.setPlayBackListener(this);
|
||||
}
|
||||
|
||||
public void play() {
|
||||
thread = new SoundThread(this, loop, filename);
|
||||
thread.start();
|
||||
paused = false;
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
thread.stop();
|
||||
paused = true;
|
||||
}
|
||||
|
||||
public void togglePause() {
|
||||
if(paused) play(); else pause();
|
||||
}
|
||||
|
||||
public void reset() { createAdvancedPlayer(); }
|
||||
|
||||
// REMEMBER TO CALL THIS FOR RESOURCE LEAKS!!!!
|
||||
public void destroy() {
|
||||
thread.stop();
|
||||
thread = null;
|
||||
// Not familiar with JLayer's API, but this seems to throw a null pointer occasionally?
|
||||
try { device.flush(); device.close(); } catch (final NullPointerException npe) { }
|
||||
try { stream.close(); } catch (final IOException e) { e.printStackTrace(); }
|
||||
player.close();
|
||||
player = null;
|
||||
filename = null;
|
||||
device = null;
|
||||
stream = null;
|
||||
}
|
||||
|
||||
private void runThread() {
|
||||
try { player.play(); // will play from start if not paused before, else it will start @ pause position
|
||||
createAdvancedPlayer();
|
||||
} catch (final JavaLayerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean paused() { return paused; }
|
||||
public String getFilename() { return filename; }
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
|
||||
private static class SoundThread extends Thread {
|
||||
|
||||
private final boolean loop;
|
||||
private final MusicPlayer player;
|
||||
|
||||
public SoundThread(final MusicPlayer player, final boolean loop, final String filename) {
|
||||
this.loop = loop;
|
||||
this.player = player;
|
||||
setName(filename);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
do {
|
||||
|
||||
player.runThread();
|
||||
|
||||
} while(loop);
|
||||
|
||||
player.destroy();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package sjgs.sound;
|
||||
|
||||
import java.io.InputStream;
|
||||
import javazoom.jl.player.FactoryRegistry;
|
||||
import javazoom.jl.player.JavaSoundAudioDevice;
|
||||
import javazoom.jl.player.advanced.AdvancedPlayer;
|
||||
import javazoom.jl.player.advanced.PlaybackListener;
|
||||
import sjgs.utils.multithreading.Runner;
|
||||
|
||||
// NOTE: This class needs the JLayer.jar in your main project in order to function!
|
||||
|
||||
public final class SoundPlayer extends PlaybackListener {
|
||||
|
||||
private static final SoundPlayer PLAYBACK_LISTENER = new SoundPlayer();
|
||||
|
||||
public static void play(final String filename) { play(filename, 0); }
|
||||
|
||||
/** @method play: Plays an mp3 audio file with given resource stream name.
|
||||
* gain can be a value from -80 to 6. No argument assumes no gain */
|
||||
public static void play(final String filename, final float gain) {
|
||||
new Runner(() -> {
|
||||
try {
|
||||
JavaSoundAudioDevice device = (JavaSoundAudioDevice) FactoryRegistry.systemRegistry().createAudioDevice();
|
||||
device.createSource();
|
||||
device.setGain(gain);
|
||||
InputStream stream = SoundPlayer.class.getResourceAsStream(filename);
|
||||
AdvancedPlayer player = new AdvancedPlayer(stream, device);
|
||||
player.setPlayBackListener(PLAYBACK_LISTENER);
|
||||
player.play();
|
||||
// --------- //
|
||||
device.flush();
|
||||
device.close();
|
||||
player.close();
|
||||
stream.close();
|
||||
device = null;
|
||||
stream = null;
|
||||
player = null;
|
||||
} catch (final Exception e) { e.printStackTrace(); }
|
||||
}, "Sound Player: " + filename).start();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user