sounddaemon.java

来自「用java实现的」· Java 代码 · 共 147 行

JAVA
147
字号
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq;

import java.io.File;
import java.io.FileInputStream;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 播放声音的精灵线程
 * 
 * @author 马若劼
 */
public class SoundDaemon extends Thread {
	// 监听Line播放结束事件,关闭line
	private class DefaultLineListener implements LineListener {
		public void update(LineEvent e) {
			if(e.getType() == LineEvent.Type.STOP)
				((Clip)e.getLine()).stop();
		}
	}
	
    // Log对象
    private static Log log = LogFactory.getLog(SoundDaemon.class);
    // lock object
    private Object lock;
    // default line listener
    private DefaultLineListener dll;
    // 停止标志
    private volatile boolean stop;
    // 本次需要播放的声音文件名
    private String filename;
    // 是否使能声音提示
    private boolean enable;
    
	/**
	 * 构造函数
	 */
	public SoundDaemon() {
		lock = new Object();
		dll = new DefaultLineListener();
		stop = false;
		enable = true;
		setName("Sound");
		setDaemon(true);
	}
	
	/**
	 * 播放一段声音
	 * @param filename 声音文件名
	 */
	public void play(String filename) {
		// 播放
		synchronized(lock) {
			this.filename = filename;
			lock.notify();
		}		
	}
	
	/**
	 * 设置停止标志
	 * @param b
	 */
	public void setStop(boolean b) {
		synchronized(lock) {
			stop = b;
			lock.notify();
		}
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		log.debug("声音播放精灵线程已经启动");
		while(!stop) {
			synchronized(lock) {
				try {
					if(stop) break;
					lock.wait();
				} catch (InterruptedException e) {
				}
				// filename不等于就播放声音
				if(enable && filename != null) {
					try {					
					 	File file = new File(filename);
					 	AudioFileFormat aff = AudioSystem.getAudioFileFormat(file);
				        AudioFormat format = aff.getFormat();
						AudioInputStream stream = new AudioInputStream(new FileInputStream(file), format, aff.getFrameLength());
				        DataLine.Info info = new DataLine.Info(Clip.class, format, ((int)stream.getFrameLength()*format.getFrameSize()));
					    Clip clip = (Clip) AudioSystem.getLine(info);
				        // This method does not return until the audio file is completely loaded
				        clip.open(stream);
				        // Start playing
				        clip.start();
						filename = null;
					} catch (Exception e) {
					}					
				}
		    }			
		}
		log.debug("声音播放精灵线程正常退出");
	}
	
	/**
	 * @return Returns the enable.
	 */
	public boolean isEnable() {
		return enable;
	}
	
	/**
	 * @param enable The enable to set.
	 */
	public void setEnable(boolean enable) {
		this.enable = enable;
	}
}

⌨️ 快捷键说明

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