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

📄 audioplaystream.java

📁 java开发的netbeans插件中的application原型
💻 JAVA
字号:
/* *	AudioPlayStream.java *//* *  Copyright (c) 2001 by Florian Bomers <florian@bome.com> * * *   This program is free software; you can redistribute it and/or modify *   it under the terms of the GNU Library General Public License as published *   by the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version. * *   This program is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *   GNU Library General Public License for more details. * *   You should have received a copy of the GNU Library General Public *   License along with this program; if not, write to the Free Software *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */package cn.edu.tsinghua.thss.talkie.audio;//import	org.jsresources.apps.am.Debug;import	java.io.*;import	javax.sound.sampled.*;// OutputStream that writes its data to the soundcard outputpublic class AudioPlayStream extends OutputStream implements LineListener {	private AudioFormat m_format;	private SourceDataLine m_line;	private int m_bufferSize;	private boolean immediateStop=false;	private LineListener m_listener;	public AudioPlayStream(AudioFormat format) {		super();		m_format=format;	}		public void setListener(LineListener ll) {		m_listener=ll;	}	public void update(LineEvent event) {		if (m_listener!=null) {			m_listener.update(event);		}	}	// opens the sound hardware	public void open() throws Exception {		DataLine.Info info = new DataLine.Info(SourceDataLine.class, m_format);		// get and open the target data line for capture.		m_line = null;		try {			m_line = (SourceDataLine) AudioSystem.getLine(info);			m_line.addLineListener(this);			m_bufferSize=(int) (m_format.getSampleRate()/4); // 125ms			// align buffer size to integral frames			m_bufferSize-=m_bufferSize % m_format.getFrameSize();			m_line.open(m_format, m_bufferSize);			m_bufferSize=m_line.getBufferSize();		} catch (LineUnavailableException ex) {			throw new Exception("Unable to open the line: "+ex.getMessage());		}		immediateStop=false;	}	public void setImmediateStop(boolean value) {		immediateStop=value;	}	public void start() throws Exception {		m_line.flush();		m_line.start();		immediateStop=false;	}	public void close() throws IOException {		close(immediateStop);	}	public void close(boolean immediately) throws IOException {		if (m_line!=null) {			if (!immediately) {				drain();			} else {				m_line.stop();				flush();			}			if (m_line!=null) {				m_line.close();				m_line=null;			}		}		m_bufferSize=0;		immediateStop=false;	}	public void write(int b) throws IOException {		byte[] hack=new byte[1];		hack[0]=(byte) b;		write(hack);	}	public void write(byte[] b, int off, int len) throws IOException {		if (m_line==null) {			throw new IOException("Output line is closed.");		}		int res=m_line.write(b, off, len);	}	public void flush() throws IOException {		if (m_line!=null) {			immediateStop=true;			m_line.flush();			synchronized (this) {				notifyAll(); // interrupt any drain going on			}		}	}	public synchronized void drain() {		if (m_line!=null && m_line.isActive()) {			long t=0;			long waitTime=AMAudioFormat.bytes2Ms(m_bufferSize, m_format)+50;			try {				wait(waitTime);			} catch (InterruptedException ie) {}		}	}	public int getBufferSize() {		return m_bufferSize;	}}/*** AudioPlayStream.java ***/

⌨️ 快捷键说明

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