128 lines
2.8 KiB
Java
128 lines
2.8 KiB
Java
package com.flaremicro.visualforecast;
|
|
|
|
import java.io.File;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.util.HashSet;
|
|
import com.flaremicro.audio.AgnosticAudioSystem;
|
|
import com.flaremicro.audio.AudioFinishedListener;
|
|
import com.flaremicro.audio.AudioPlaylist;
|
|
|
|
public class AudioManager implements AudioFinishedListener {
|
|
|
|
private PropertyManager propertyManager;
|
|
private AgnosticAudioSystem aas;
|
|
private AudioPlaylist audioPlaylist;
|
|
|
|
private File playlistDirectory = new File("./Music");
|
|
|
|
HashSet<String> playingKeys = new HashSet<String>();
|
|
|
|
public AudioManager(PropertyManager propertyManager) {
|
|
this.propertyManager = propertyManager;
|
|
this.aas = new AgnosticAudioSystem();
|
|
this.aas.addAudioFinishedListener(this);
|
|
this.audioPlaylist = new AudioPlaylist(aas);
|
|
updatePlaylist();
|
|
}
|
|
|
|
private void updatePlaylist() {
|
|
playlistDirectory = propertyManager.getFile("music-dir", playlistDirectory);
|
|
if (playlistDirectory == null || !playlistDirectory.isDirectory())
|
|
return;
|
|
File[] files = playlistDirectory.listFiles();
|
|
URL[] urls = new URL[files.length];
|
|
for (int i = 0; i < files.length; i++)
|
|
{
|
|
try
|
|
{
|
|
urls[i] = files[i].toURI().toURL();
|
|
}
|
|
catch (MalformedURLException e)
|
|
{
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
audioPlaylist.setPlaylist(urls);
|
|
audioPlaylist.shuffle();
|
|
}
|
|
|
|
public void audioFinished(String key, URL resource) {
|
|
playingKeys.remove(key);
|
|
if (playingKeys.size() == 0)
|
|
rampVolume(1F);
|
|
}
|
|
|
|
public void startPlaylist() {
|
|
if (playlistDirectory == null || !playlistDirectory.isDirectory())
|
|
return;
|
|
audioPlaylist.start();
|
|
}
|
|
|
|
public void stopPlaylist() {
|
|
audioPlaylist.stop();
|
|
}
|
|
|
|
public void end() {
|
|
audioPlaylist.stop();
|
|
aas.destroy();
|
|
}
|
|
|
|
public void playAnnouncement(File file) {
|
|
try
|
|
{
|
|
URL url = file.toURI().toURL();
|
|
rampVolume(-10F);
|
|
playingKeys.add(aas.play(url));
|
|
}
|
|
catch (MalformedURLException e)
|
|
{
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
Thread currThread = null;
|
|
private float volume = 1F;
|
|
|
|
//***horrible***
|
|
private void rampVolume(float newVolume) {
|
|
this.volume = newVolume;
|
|
if (currThread != null)
|
|
return;
|
|
currThread = new Thread(new Runnable() {
|
|
public void run() {
|
|
|
|
try
|
|
{
|
|
if (volume < audioPlaylist.getVolume())
|
|
{
|
|
while (audioPlaylist.getVolume() > volume)
|
|
{
|
|
audioPlaylist.setVolume(audioPlaylist.getVolume() - 0.1F);
|
|
Thread.sleep(10L);
|
|
}
|
|
}
|
|
else if (volume > audioPlaylist.getVolume())
|
|
{
|
|
while (audioPlaylist.getVolume() < volume)
|
|
{
|
|
audioPlaylist.setVolume(audioPlaylist.getVolume() + 0.1F);
|
|
Thread.sleep(10L);
|
|
}
|
|
}
|
|
}
|
|
catch (InterruptedException ex)
|
|
{
|
|
|
|
}
|
|
audioPlaylist.setVolume(volume);
|
|
currThread = null;
|
|
}
|
|
|
|
});
|
|
currThread.start();
|
|
}
|
|
|
|
}
|