⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audioplayer.java

📁 Applet录音机源码,Java程序
💻 JAVA
字号:
package sunstudio.record.au.audio.play;

import java.io.*;
import java.util.*;
import sunstudio.util.*;

public class AudioPlayer extends InputStream implements Runnable{

	private boolean pause=false;
	private InputStream input;
	Thread td=null;
	sun.audio.AudioPlayer player=sun.audio.AudioPlayer.player;
	boolean isover=false;
    protected static final int DFLT_SIZE=4096;
    protected int putPTR;
    protected int getPTR;
    protected int waitingPuts;
    protected int waitingGets;
    protected int emptyBytes;
    protected int usedBytes;
    protected Object putMonitor;
    protected Object getMonitor;
    protected byte data[];
    protected int moduloMask_;
    protected boolean atEOF;
    protected boolean gaveEOF;
    protected boolean flushed_;
	private Vector b1=new Vector();
	
	public AudioPlayer(){
		super();
		putMonitor=new Object();
		getMonitor=new Object();
        gaveEOF=false;
        flushed_=false;
        data=new byte[4096];
        moduloMask_=4096-1;
        emptyBytes=4096;
        atEOF=false;
	}
	public void addAudioPlayListener(AudioPlayListener l){b1.addElement(l);}
	synchronized void notifyListener(int evtType,byte[] wavedata){
		AudioPlayEvent evt=new AudioPlayEvent(evtType,wavedata);
		for(Enumeration enu=b1.elements();enu.hasMoreElements();)
			((AudioPlayListener)enu.nextElement()).onAudioPlayEvent(evt);
	}
////////////////////////////////////////////////////
	public void run(){
		try{
			byte[] tmpp=new byte[800];
			int readCount=0;
			while((readCount=SystemTool.readBytes(input,800,tmpp))>-1)write(tmpp,0,800);
			notifyListener(AudioPlayEvent.PLAY_OVER,null);
		}catch(Exception e){
			clearData();
			notifyListener(AudioPlayEvent.PLAY_ERR,null);
		}
		stop_play();
	}
	public void suspend_play(){
		pause=true;
	}
	public void resume_play(){
		if(!pause)return;
		pause=false;
	}
    public void stop_play(){
		try{
			clearData();
			if(td!=null){
				td.stop();
				td=null;
			}
		}catch(Exception e){e.printStackTrace();}	
		try{
			player.stop(this);
		}catch(Exception e){}
		synchronized(putMonitor){
			try{
				if(waitingPuts>0)putMonitor.notifyAll();
			}catch(Exception e){e.printStackTrace();}
		}
		synchronized(getMonitor){
			try{
				if(waitingGets>0)getMonitor.notifyAll();
			}catch(Exception e){e.printStackTrace();}
		}
    }
	public void clearData(){
        gaveEOF=false;
        flushed_=false;
        int j=4096;
		synchronized(data){
			data=null;
			data=new byte[j];
		}
        moduloMask_=j-1;
        emptyBytes=j;
        atEOF=false;
	}
    public void start_play(InputStream inp){
		input=inp;
		try{
			stop_play();
			pause=false;
			isover=false;
			player.start(this);
			td=null;
			td=new Thread(this);
			td.start();
		}catch(Exception e){}
    }	
	int innerRead()throws Exception{
		/*int rt=-1;
		while(true){
			if(!readOver){
				rt=input.read();
				c++;
				readOver=(c==processlen);
				return rt;
			}else{
				if(c==processlen){
					rt=input.read();
					if(rt==-1)return -1;//READ_STREAM_OVER
					c=0;
					readOver=(rt==0);
				}else{
					c++;
					return rt(c-1);
				}
			}
		}*/
		return input.read();
	}
	int innerRead(byte abyte0[], int i, int j)throws Exception{
        if(j<=0)return 0;
        int k=innerRead();
        if(k==-1)return -1;
        abyte0[i]=(byte)k;
        int i1=1;
        try{
            while(i1<j){
                int l=innerRead();
                if(l==-1)break;
                if(abyte0!=null)abyte0[i+i1]=(byte)l;
                i1++;
            }
        }catch(IOException _ex){}
        return i1;
	}
    public static int Au2Wave(byte byte0){
        byte0^=0xff;
        int i=((byte0&0xf)<<3)+132;
        i<<=(byte0&0x70)>>4;
		return (i-132)*((byte0&0x80)==0?-1:1);
        //if((byte0&0x80)==0)return 132-i;
        //else return i-132;
    }
	public static byte Wave2Au(int wv){
		int i2=Math.abs(wv);
		if(i2>32635)i2=32635;
		i2+=132;
        int x=ULAW_LUT[i2>>7];
        int y=i2>>x+3&0xf;
		i2=(x<<4|y)^(wv>0?0xff:0x7f);
		return (byte)i2;
	}
	////////////////////////////////////////////////////////////////////////
    private void put(byte byte0){
		synchronized(putMonitor){
            /*if(atEOF)return;
            emptyBytes--;
            data[putPTR]=byte0;
            putPTR=(putPTR + 1) % data.length;
			*/
			synchronized(getMonitor){
				usedBytes++;
				isover=true;
				if(waitingGets>0)getMonitor.notify();
			}
		}
    }
    private void put(byte abyte0[],int i,int j){
        int k=j;
        synchronized(putMonitor){
            while(emptyBytes<j&&!atEOF) {
                waitingPuts++;
                try{
                    putMonitor.wait();
                }catch(InterruptedException _ex){}
                waitingPuts--;
            }
            if(atEOF)return;
            emptyBytes-=j;
            if(putPTR+j>data.length){
                k=data.length-putPTR;
                System.arraycopy(abyte0,i,data,putPTR,k);
                i+=k;
                putPTR=0;
                k=j-k;
            }
            System.arraycopy(abyte0,i,data,putPTR,k);
            putPTR+=k;
        }
        synchronized(getMonitor){
            usedBytes+=j;
            if(waitingGets>0)getMonitor.notify();
        }
    }
    private int take(byte abyte0[],int i,int j){
        synchronized(getMonitor){
            while(usedBytes<j && !atEOF && !flushed_){
				if(isover)return -2;//update for play over
                waitingGets++;
                try{
                    getMonitor.wait();
                }catch(InterruptedException _ex){}
		        waitingGets--;
            }
            if(j>usedBytes)j=usedBytes;
            if(atEOF && usedBytes <= 0){
                byte byte0=-1;
                return byte0;
            }
            if(flushed_)flushed_=false;
            usedBytes-=j;
            int k=j;
            if(getPTR + j>data.length){
                k=data.length-getPTR;
                System.arraycopy(data,getPTR,abyte0,i,k);
                getPTR=0;
                i+=k;
                k=j-k;
            }
            System.arraycopy(data,getPTR,abyte0,i,k);
            getPTR+=k;
        }
        synchronized(putMonitor){
            emptyBytes+=j;
            if(waitingPuts>0)putMonitor.notify();
        }
        return j;
    }
    public int read() throws IOException{throw new IOException();}
    public int read(byte abyte0[],int i,int j) throws IOException{
		/////////////////
		if(pause){//update for pause
			abyte0[0]=127;
			return 1;
		}
//		if(usedBytes<=0)throw new IOException();
        int k=0;
        int l=0;
        if(gaveEOF)throw new EOFException("pipe is already closed");
        int i1;
        do{
            i1=Math.min(data.length,j);
            k=take(abyte0,i,i1);
			if(k==-2)throw new IOException();//流已到尾update for play over
            if(k==-1){
                if(l==0){
                    gaveEOF=true;
                    return -1;
                }
                break;
            }
            i+=k;
            l+=k;
            j-=k;
		}while(i1==k&&j>data.length);
		try{
			notifyListener(AudioPlayEvent.PLAY_DATA,abyte0);
		}catch(Exception e){}
        return l;
    }
    public void receivedFlush(){
        if(atEOF)return;
        synchronized(getMonitor){
            flushed_=true;
            if(waitingGets>0)getMonitor.notifyAll();
        }
    }
    public void receive(int i) throws EOFException{
        if(atEOF)throw new EOFException("stream is closed");
        if(emptyBytes==0)receivedFlush();
        put((byte)(i&0xff));
    }
    public void receive(byte abyte0[],int i,int j)throws EOFException{
        while(j>data.length && !atEOF){
            put(abyte0,i,data.length);
            j-=data.length;
            i+=data.length;
        }
        if(!atEOF){
            if(j <= 0)return;
            if(j>emptyBytes)receivedFlush();
            put(abyte0,i,j);
            return;
        }else throw new EOFException("stream is closed");
    }
	public void write(int i) throws IOException{receive(i);}
    public void write(byte abyte0[], int i, int j) throws IOException{
		receive(abyte0, i, j);
	}
    private static final int ULAW_LUT[] = {
        0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 
        3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 
        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
        5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 
        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
        6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
        7, 7, 7, 7, 7, 7
    };
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -