package com.zaranux.os.client.programs;
import java.util.HashMap;
import java.util.Set;
import com.allen_sauer.gwt.voices.client.Sound;
import com.allen_sauer.gwt.voices.client.SoundController;
import com.allen_sauer.gwt.voices.client.handler.PlaybackCompleteEvent;
import com.allen_sauer.gwt.voices.client.handler.SoundHandler;
import com.allen_sauer.gwt.voices.client.handler.SoundLoadStateChangeEvent;
import com.openmashupos.core.client.Mashlet;
import com.zaranux.client.api.AsyncCallback;
import com.zaranux.client.app.core.Proxy;
import com.zaranux.client.app.core.ZaranuxProxy;
import com.zaranux.os.client.core.Program;
import com.zaranux.os.client.util.Log;
public class Player extends Program{
private final static HashMap<String, String> types = new HashMap<String, String>();
private final SoundController soundController = new SoundController();
// paths
public Player(String[] args)
{
this.args = args;
}
public Player()
{
}
private void fillTypes() {
types.put("au", Sound.MIME_TYPE_AUDIO_BASIC);
types.put("snd", Sound.MIME_TYPE_AUDIO_BASIC);
types.put("mp3", Sound.MIME_TYPE_AUDIO_MPEG);
types.put("mp2", Sound.MIME_TYPE_AUDIO_MPEG);
types.put("mp1", Sound.MIME_TYPE_AUDIO_MPEG);
types.put("mpeg", Sound.MIME_TYPE_AUDIO_MPEG);
types.put("aif", Sound.MIME_TYPE_AUDIO_X_AIFF);
types.put("midi", Sound.MIME_TYPE_AUDIO_X_MIDI);
types.put("mid", Sound.MIME_TYPE_AUDIO_X_MIDI);
types.put("wav", Sound.MIME_TYPE_AUDIO_X_WAV);
}
public String getMimeType(String ext) {
Set<String> keys = types.keySet();
for (String key : keys) {
if(key.equalsIgnoreCase(ext))
return types.get(key);
}
return Sound.MIME_TYPE_AUDIO_MPEG;
}
String[] files;
int index = 0; // current file
@Override
// args = list of files to be played
protected void main(String[] args)
{
Log.debug("Player Main");
files = args;
fillTypes();
playNext(new AsyncCallback<Boolean>()
{
public void onSuccess(Boolean b)
{
//callback.onSuccess(b);
}
public void onFailure(Throwable t)
{
//callback.onFailure(t);
}
});
}
Sound activeSound ;
public void playNext(final AsyncCallback<Boolean> callback)
{
if(index >= files.length) callback.onSuccess(true); // done ; stop condition for recursive operation
else
{
final String ext = files[index].substring(files[index].lastIndexOf(".")+1);
systemcall.delegate(null, "zaranux.com", null, -1, new AsyncCallback<String>()
{
public void onSuccess(String token)
{
//String url = "/@" + files[index] + "?" +"identity=" + systemcall.userid() + "&identityAssertion=" + token;
String path = files[index];
String url = (path.startsWith("@") || path.startsWith("/@")) ? path : ("/@" + path); // + "?" +"identity=" + systemcall.userid() + "&identityAssertion=" + token;
String identity = systemcall.userid();
if(identity != null && !identity.equals(""))
{
url += "?" +"identity=" + systemcall.userid() + "&identityAssertion=" + token;
}
//Mashlet mashlet = new Mashlet(url);
//mashlet.setWidth("500px");
//mashlet.setPixelSize(500, 400);
//setWidget(mashlet);
String mimeType = getMimeType(ext);
activeSound = soundController.createSound(mimeType,url,true);
// add a sound handler so we know when the sound has loaded/completed
activeSound.addEventHandler(new SoundHandler()
{
public void onPlaybackComplete(PlaybackCompleteEvent event) {
index++; // pint to the next file
playNext(callback);
}
public void onSoundLoadStateChange(SoundLoadStateChangeEvent event) {
}
});
activeSound.play();
}
public void onFailure(Throwable caught)
{
setStatus("" + caught);
}
}
);
}
}
// to be overriden by programs
protected void finalize()
{
activeSound.stop();
}
}