📄 dataplay.java
字号:
package skullplay;
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;
public class DataPlay extends Thread implements LineListener
{
private byte[] databyte;
private int datasize,con=1,listener=1;
private AudioInputStream soundStream;
private SourceDataLine soundline;
private AudioFormat soundformat;
PlayEvent pe1;
PlayListener pl;
private String playevent1="END";
PlayThread pt=new PlayThread();
public void DataPlay()
{}
public void dataclose()
{
if(listener==1)
{
soundline.stop();
}
if(listener==2)
{
listener=1;
soundline.removeLineListener(this);
soundline.stop();
soundline.close();
//soundline=null;
}
soundStream=null;
soundformat=null;
con=2;
}
public void play(AudioInputStream soundStream,AudioFormat soundformat,SourceDataLine soundline)
{
this.soundStream=soundStream;
this.soundline=soundline;
soundline.addLineListener(this);
listener=2;
databyte=new byte[204800];
try
{
if(soundStream.markSupported())
{
soundStream.mark(0);
soundStream.reset();
}
}
catch (Exception e)
{
}
try
{
soundline.open(soundformat,20480);//通知音频线将要放入音频数据的格式,并设置缓存!
}
catch(LineUnavailableException elua1)
{
System.out.println("line open:存得不到控制!\n"+elua1);
System.exit(0);
}
catch(IllegalArgumentException eia1)
{
System.out.println("line open:缓冲不能放入数据!\n"+eia1);
System.exit(0);
}
catch(IllegalStateException eis)
{
System.out.println("line open:以被打开了!\n"+eis);
System.exit(0);
}
catch(SecurityException es1)
{
System.out.println("line opne:数据不安全!\n"+es1);
System.exit(0);
}
if(soundline.isOpen())
{
soundline.start();
this.start();
}
else
{
System.out.println("it can not open!");
System.exit(0);
}
}
public void run()
{
pt.start();
do
{
playback();//读完soundbyte的数据后,再到playback()方法里再进行读取数据!
soundline.write(databyte,0,datasize);//往缓存里写入数据!
if(con==2)
{
soundline.stop();
}
}while(con==1);
}
private void playback()
{
try
{
if(soundStream.available()>102400)
{
datasize=soundStream.read(databyte);
}
else if(soundStream.available()<=0)
{
soundline.stop();
con=2;
}
else
{
soundStream.read(databyte,0,soundStream.available());
datasize=soundStream.available();
datasize-=datasize%4;
con=2;
}
}
catch (IOException eio7)
{
System.out.println("data->byte:I/O出现异常!\n"+eio7);
System.exit(0);
}
}
class PlayThread extends Thread
{
public PlayThread()
{}
public void run()
{
int position,old=0;
for (;listener==2 ; )
{
try
{
position=soundline.getFramePosition();
}
catch(NullPointerException enp)
{
position=old;
System.out.println("position is null!\n"+enp);
}
if(position-old==0 && position>0 && old>0)
{
con=2;
old=0;
System.out.println("3");
break;
}
else
{
old=position;
}
try
{
this.sleep(1000);
}
catch (InterruptedException eipt)
{
System.out.println("sleep:不能转为睡眠!\n"+eipt);
}
}
if(listener==2)
{
soundline.close();
}
}
};
public void update(LineEvent event)
{
if(event.getType()==LineEvent.Type.CLOSE)
{
this.yield();
pt.yield();
soundline=null;
pe1=new PlayEvent(playevent1);//开始发生事件!
System.out.println("1");
firePlayEvent(pe1);
soundline.removeLineListener(this);
listener=1;
System.gc();
}
}
public void firePlayEvent(PlayEvent pe)
{
pl.validatePlay(pe);
}
public void addPlayListener(PlayListener pl)
{
this.pl=pl;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -