84 lines
1.6 KiB
Java
84 lines
1.6 KiB
Java
package com.flaremicro.audio;
|
|
|
|
import java.net.URL;
|
|
import java.util.Random;
|
|
|
|
public class AudioPlaylist implements AudioFinishedListener{
|
|
|
|
|
|
private final AgnosticAudioSystem aas;
|
|
|
|
public AudioPlaylist(AgnosticAudioSystem aas)
|
|
{
|
|
this.aas = aas;
|
|
}
|
|
|
|
String currentKey = null;
|
|
int currentPlaylistIndex = 0;
|
|
URL[] playlist = null;
|
|
|
|
float currVol = 1F;
|
|
private boolean stopped = true;
|
|
|
|
public void setPlaylist(URL[] url)
|
|
{
|
|
this.playlist = url;
|
|
this.currentPlaylistIndex = 0;
|
|
}
|
|
|
|
public void shuffle(){
|
|
Random rand = new Random();
|
|
|
|
for (int i = 0; i < playlist.length; i++) {
|
|
int randomIndexToSwap = rand.nextInt(playlist.length);
|
|
URL temp = playlist[randomIndexToSwap];
|
|
playlist[randomIndexToSwap] = playlist[i];
|
|
playlist[i] = temp;
|
|
}
|
|
}
|
|
|
|
public void start()
|
|
{
|
|
stopped = false;
|
|
currentPlaylistIndex = 0;
|
|
aas.addAudioFinishedListener(this);
|
|
if(currentKey == null && playlist != null && playlist.length > 0)
|
|
{
|
|
currentKey = aas.play(playlist[0]);
|
|
aas.setVolume(currVol, currentKey);
|
|
}
|
|
}
|
|
|
|
public void next()
|
|
{
|
|
if(playlist != null && playlist.length > 0)
|
|
{
|
|
currentPlaylistIndex = (currentPlaylistIndex + 1 ) % playlist.length;
|
|
currentKey = aas.play(playlist[currentPlaylistIndex]);
|
|
aas.setVolume(currVol, currentKey);
|
|
}
|
|
}
|
|
|
|
public void audioFinished(String key, URL resource) {
|
|
if(!stopped && key == currentKey)
|
|
{
|
|
next();
|
|
}
|
|
}
|
|
|
|
public void setVolume(float volume) {
|
|
currVol = volume;
|
|
aas.setVolume(currVol, currentKey);
|
|
}
|
|
|
|
public void stop() {
|
|
stopped = true;
|
|
aas.stop(currentKey);
|
|
}
|
|
|
|
public float getVolume() {
|
|
return currVol;
|
|
}
|
|
|
|
}
|