/*
* 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.util.Arrays;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
/**
*
*
*/
public class JavaSoundAudioPlayer implements FileAudioPlayer,InputStreamAudioPlayer{
public static Log log=LogFactory.getLog(JavaSoundAudioPlayer.class);
protected AudioStopper stopper;
public AudioStopper getStopper() {
return stopper;
}
public void setStopper(AudioStopper stopper) {
this.stopper = stopper;
}
protected int buffer=1024;
public int getBuffer() {
return buffer;
}
public void setBuffer(int buffer) {
this.buffer = buffer;
}
/* (non-Javadoc)
* @see org.jpn.xucker.commons.audio.sp.FileAudioPlayer#play(java.io.File)
*/
public void play(File file) {
try {
log.trace("play-file:"+file.getAbsolutePath());
log.trace("file-lehgth:"+file.length());
// TODO Auto-generated method stub
play(new FileInputStream(file));
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see org.jpn.xucker.commons.audio.sp.AudioInputStreamPlayer#play(javax.sound.sampled.AudioInputStream)
*/
public void play(InputStream stream) {
try{
AudioInputStream inputStream=AudioSystem.getAudioInputStream(stream);
//AudioInputStream inputStream =
// AudioSystem.getAudioInputStream(audioFile);
AudioFormat format =inputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
int readBytes = 0;
byte[] bytedata = new byte[1024];
while (readBytes != -1) {
if(stopper!=null && stopper.isStopped()==true){
break;
}
readBytes = inputStream.read(bytedata, 0, bytedata.length);
if (readBytes >= 0) {
//int writeBytes =
line.write(bytedata, 0, readBytes);
}
}
line.drain();
line.close();
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static long countByteAt(AudioFormat format,long millisecond){
if(millisecond==0){
return 0;
}
long rateIndex=(int)(format.getFrameRate()/1000*millisecond);
return rateIndex*format.getSampleSizeInBits()/8*format.getChannels();
}
public void play(File file,long startmillisecond,long endmillisecond) {
try {
play(new FileInputStream(file),startmillisecond,endmillisecond);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void play(InputStream stream,long startmillisecond,long endmillisecond) {
// System.out.println(startmillisecond+","+endmillisecond);
try{
AudioInputStream audioInputStream=null;
audioInputStream=AudioSystem.getAudioInputStream(stream);
//System.out.println(audioInputStream.getClass().getName());
//AudioInputStream inputStream =
// AudioSystem.getAudioInputStream(audioFile);
AudioFormat format =audioInputStream.getFormat();
long startByte=countByteAt(format,startmillisecond);
// System.out.println(startByte);
long endByte=countByteAt(format,endmillisecond);
long playByte=endByte-startByte;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
//skip
audioInputStream.skip(startByte);
int readBytes = 0;
int buffersize=256;
byte[] bytedata = new byte[buffersize];
long readedByte=0;
while (readBytes != -1) {
if(stopper!=null && stopper.isStopped()==true){
break;
}
readBytes = audioInputStream.read(bytedata, 0, bytedata.length);
if (readBytes >= 0) {
//int writeBytes =
if(readedByte+readBytes<playByte){
line.write(bytedata, 0, readBytes);
}else{
//byte[] tmpbyte=new byte[buffersize];
//int min=Math.min(0,(int)(playByte-readedByte));
//System.arraycopy(bytedata,0,tmpbyte,0,min);
//line.write(tmpbyte, 0, readBytes); //about
line.write(bytedata, 0, Math.min((int)(playByte-readedByte),readBytes));
break;
}
readedByte+=readBytes;
}
}
//for final push?
byte[] tmpbyte = new byte[1024*32]; //i don't know that it is best tmp output size.
line.write(tmpbyte,0,tmpbyte.length);
//System.out.println(readedByte);
line.drain();
line.stop();
line.close();
line=null;
audioInputStream.close();
audioInputStream=null;
info=null;
//stream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
|