/*
* Created on 2004/10/03
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.commons.audio;
import java.applet.AudioClip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
*
*/
public class AudioClipAudioPlayer implements FileAudioPlayer,InputStreamAudioPlayer{
AudioClip audioClip;
File tmp;
public void play(File file) {
try {
URL url;
url = new URL("file:///"+file.getAbsolutePath().replaceAll("\\\\","/"));
if(audioClip!=null){
audioClip.stop();
audioClip=null;
}
audioClip = java.applet.Applet.newAudioClip(url);
audioClip.play();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see org.jpn.xucker.commons.audio.InputStreamAudioPlayer#play(java.io.InputStream)
*/
public void play(InputStream input) {
synchronized(this){
try {
if(tmp!=null){
tmp.delete();
}
// TODO Auto-generated method stub
tmp=File.createTempFile("tmp",".wav");
BufferedInputStream binput=new BufferedInputStream(input);
BufferedOutputStream bout=new BufferedOutputStream(new FileOutputStream(tmp));
int c;
while((c=binput.read())!=-1){
bout.write(c);
}
binput.close();
bout.close();
play(tmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
|