📄 wavplayer.java
字号:
/*
* Created on 2005-9-27 by pcy
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package a.a.a.mmedia;
import javax.microedition.media.MediaException;
import javax.microedition.media.PlayerListener;
import javax.sound.sampled.*;
public class WavPlayer extends BasicPlayer implements Runnable {
private Clip clip;
//private SourceDataLine line;
private Thread thread;
AudioInputStream ais;
//byte[] data;
public WavPlayer() {
super();
}
protected void doRealize() throws MediaException {
try{
if(stream!=null){
ais=AudioSystem.getAudioInputStream(stream);
/*if(data==null){
data=new byte[ais.available()];
}
ais.read(data);*/
AudioFormat format=ais.getFormat();
DataLine.Info info=new DataLine.Info(Clip.class,format,
(int)ais.getFrameLength()*format.getFrameSize());
clip=(Clip)AudioSystem.getLine(info);
info=null;
clip.open(ais);
/*DataLine.Info info2=new DataLine.Info(SourceDataLine.class,format,
(int)ais.getFrameLength()*format.getFrameSize());
line=(SourceDataLine)AudioSystem.getLine(info2);
line.open(format);*/
ais=null;
}
}catch(Exception e){
throw new MediaException(e.getMessage());
}
}
protected void doPrefetch() throws MediaException {
}
protected boolean doStart() {
if(thread!=null){
thread.interrupt();
thread=null;
}
thread=new Thread(this);
thread.start();
return true;
}
protected void doStop() {
if(clip!=null&&clip.isRunning()){
clip.stop();
}
}
protected void doDeallocate() {
if(clip!=null&&clip.isOpen()){
clip.close();
}
clip=null;
if(thread!=null){
thread.interrupt();
thread=null;
}
}
protected void doClose() {
if(clip!=null){
if(clip.isOpen()){
clip.close();
}
}
}
protected long doSetMediaTime(long now) throws MediaException {
clip.setMicrosecondPosition(now);
return clip.getMicrosecondPosition();
}
protected long doGetMediaTime() {
return clip.getMicrosecondPosition();
}
protected long doGetDuration() {
return clip.getMicrosecondLength();
}
protected javax.microedition.media.Control doGetControl(String type) {
if ((getState() >= REALIZED) &&
type.equals("javax.microedition.media.control.VolumeControl")) {
return this;
}
return null;
}
protected int doSetLevel(int vol) {
if(clip.isControlSupported(FloatControl.Type.VOLUME)){
((FloatControl)clip.getControl(FloatControl.Type.VOLUME)).setValue((float)vol/100);
return vol;
}else{
if(clip.isControlSupported(BooleanControl.Type.MUTE)){
if(vol<=0){
((BooleanControl)clip.getControl(BooleanControl.Type.MUTE)).setValue(true);
}else if(vol>0){
((BooleanControl)clip.getControl(BooleanControl.Type.MUTE)).setValue(false);
}
}
return (int)(clip.getLevel()*100);
}
}
public void run() {
if(clip!=null&&clip.isOpen()){
clip.start();
while (clip.isActive() && thread != null) {
try {
Thread.sleep(300);
}catch (Exception e) {
break;
}
}
this.sendEvent(PlayerListener.END_OF_MEDIA,null);
}
if(thread!=null){
thread.interrupt();
thread=null;
}
}
public String getContentType() {
chkClosed(true);
return "audio/x-wav";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -