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

📄 soundapplet.java.bak

📁 java的书上例子
💻 BAK
字号:
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

/** 一个可以选择播放音频文件的小应用程序,并可以同时选择多个音频文件合成播放 
  * @作者:尉哲明
  * @日期:2001年5月 */

/** JApplet类 */
public class SoundApplet extends JApplet implements ActionListener
{
	private String[] audioFile={"bird.au",
								"bark.aiff",
								"blesh.mid",
								"classical.rmf",
								"bg2.wav"}; //声音文件数组
	private JComboBox chosenAudioFileBox;//定义下拉菜单,用于选择播放不同的音频文件
	private int chosenIndex;		//被选择的音频文件指针
	private SoundTable soundTable;//存放“音频文件名--- AudioClip对象”对的哈希表
	private AudioClip play,loop;
	private AudioClip synthesize[];//记录合成播放的音频文件所对应的AudioClip对象数组
	private JButton playButton,loopButton,stopButton,synthesizeButton;
	private TextArea synthesizeAudioFile;//定义文本区,用于显示合成播放的音频文件名
	private boolean looping=false,synthesizing=false;//定义循环播放和合成播放的标志
	private URL soundFileBaseURL;//声音文件基址
	private String synthesizedFileArray[];//记录合成播放的音频文件名的数组
	private int synthesizeIndex=0; //合成播放的音频文件名的数组指针(计数器)
	private String newline;//系统行间隔属性
	
	/** init()方法 */
	public void init()
	{
		soundLoad();//下载各音频文件
		chosenAudioFileBox=new JComboBox(audioFile);//初始化下拉菜单
		chosenAudioFileBox.setSelectedIndex(0);
		chosenIndex=chosenAudioFileBox.getSelectedIndex();
		chosenAudioFileBox.addActionListener(this);
		playButton=new JButton("Play");
		playButton.addActionListener(this);
		loopButton=new JButton("Loop");
		loopButton.addActionListener(this);
		stopButton=new JButton("Stop");
		stopButton.addActionListener(this);
		stopButton.setEnabled(false);
		synthesizeButton=new JButton("Synthesize");
		synthesizeButton.addActionListener(this);
		synthesizeAudioFile=new TextArea(6,20);
		synthesizeAudioFile.setEditable(false);
		JPanel panel=new JPanel();
		panel.setLayout(new FlowLayout(FlowLayout.CENTER,5,40));
		panel.add(chosenAudioFileBox);
		panel.add(playButton);
		panel.add(loopButton); 
		panel.add(stopButton); 
		panel.add(synthesizeButton);
		panel.add(synthesizeAudioFile);
		setContentPane(panel);	
		int len=audioFile.length;
		synthesize=new AudioClip[len];//初始化合成播放的音频文件所对应的AudioClip对象数组
		synthesizedFileArray=new String[len];//初始化合成播放的音频文件名的数组
		newline=System.getProperty("line.separator");
					//获取系统的行间隔属性
	}//init()方法结束
	
	/** soundLoad()方法,用来下载各音频文件 */
	public void soundLoad()
	{
		soundTable=new SoundTable(audioFile.length);//创建并初始化哈希表
		soundFileBaseURL=getCodeBase();//取得声音文件基址
		for(int i=0;i<audioFile.length;i++)
		{
			new SoundFileLoadThread(this,soundTable,soundFileBaseURL,audioFile[i]).start();//建立声音文件下载线程并启动该线程
		}
	}//soundLoad()方法结束
	
	/** start()方法,当用户返回该网页时,声音恢复播放 */
	public void start()
	{
		if (looping)//循环播放标志
		{
			loop.loop();
		}
		if(synthesizing)//合成播放标志
		{
			String fileName;
			for(int i=0;i<synthesizeIndex;i++)
			{
				fileName=synthesizedFileArray[i];//音频文件名
				synthesize[i]=null;
				synthesize[i]=soundTable.getClip(fileName);//从哈希表中查到对应的AudioClip对象
				if (synthesize[i]!=null)
				{
					synthesize[i].loop();
					showStatus("Synthesizing sound.");
				}
				else
				{
					showStatus("Sound "+fileName+" not loaded yet. Synthesize failed.");
				}
			}
		}
	}//start()方法结束
	
	/** stop()方法,当用户离开该网页时,终止播放音频文件 */
	public void stop()
	{
		if(play!=null)
			play.stop();
		if (looping)
		{
			loop.stop();
		}
		if(synthesizing)
		{
			for(int i=0;i<synthesizeIndex;i++)
			{
				if(synthesize[i]!=null)
					synthesize[i].stop();
			}
		}
	}//stop()方法结束
	
	/** 相应的动作事件处理方法 */
	public void actionPerformed(ActionEvent e)
	{
		Object source=e.getSource();
		if (source==chosenAudioFileBox)
		{//在下拉菜单中选择
			chosenIndex=chosenAudioFileBox.getSelectedIndex();
			new SoundFileLoadThread(this,soundTable,soundFileBaseURL,audioFile[chosenIndex]).start();
			if (synthesizing)//合成音频标志为真
			{
				synthesizedFileArray[synthesizeIndex]=audioFile[chosenIndex];//记录选择的合成音频文件
				synthesizeIndex++;        //计数器加1
				String text="";
				for(int i=0;i<synthesizeIndex;i++)
				{
					text=text+"   "+synthesizedFileArray[i]+newline;
				}
				synthesizeAudioFile.setText(text);//将选择的合成音频文件名显示在文本区中
			}
		}
		if (source==playButton)
		{//单击play按钮
			String chosenFile=audioFile[chosenIndex];
			play=soundTable.getClip(chosenFile);
			if (play!=null)
			{
				play.play();
				stopButton.setEnabled(true);
				loopButton.setEnabled(false);
				synthesizeButton.setEnabled(false);
				showStatus("Playing sound "+chosenFile+".");
			}
			else
			{
				showStatus("Sound "+chosenFile+" not loaded yet.");
			}
			return;
		}
		if (source==loopButton)
		{//单击loop按钮
			String chosenFile=audioFile[chosenIndex];
			loop=soundTable.getClip(chosenFile);
			if (loop!=null)
			{
				looping=true;//设置循环播放标志
				loop.loop();
				stopButton.setEnabled(true);
				playButton.setEnabled(false);
				synthesizeButton.setEnabled(false);
				showStatus("Playing sound "+chosenFile+" continuously.");
			}
			else
			{
				showStatus("Sound "+chosenFile+" not loaded yet.");
			}
			return;
		}
		if (source==stopButton)
		{//单击stop按钮
			if (looping)
			{
				looping=false;//清除循环播放标志
				loop.stop();
				stopButton.setEnabled(false);
				playButton.setEnabled(true);
				synthesizeButton.setEnabled(true);
			}
			else if(synthesizing)
			{
				synthesizing=false;// 清除合成播放标志
				for(int i=0;i<synthesizeIndex;i++)
				{
					if(synthesize[i]!=null)
						synthesize[i].stop();
				}
				synthesizeAudioFile.setText("");//清除文本显示区
				synthesizeIndex=0; //将合成播放音频文件计数器清0
				stopButton.setEnabled(false);
				playButton.setEnabled(true);
				loopButton.setEnabled(true);
			}
			else if (play!=null)
			{
				play.stop();
				stopButton.setEnabled(false);
				loopButton.setEnabled(true);
				synthesizeButton.setEnabled(true);
			}
			stopButton.setEnabled(false);
            showStatus("Stopped.");
			return;
		}
		if (source==synthesizeButton)
		{//单击synthesize按钮
			if (synthesizing)
			{// 标志synthesizing为真,是第二次单击synthesize按钮
				String fileName;
				for(int i=0;i<synthesizeIndex;i++)
				{
					fileName=synthesizedFileArray[i];//取得音频文件名
					synthesize[i]=null;
					synthesize[i]=soundTable.getClip(fileName);//从哈希表中查到相应的AudioClip对象
					if (synthesize[i]!=null)
					{
						synthesize[i].loop();
						showStatus("Synthesizing sound.");
					}
					else
					{
						showStatus("Sound "+fileName+" not loaded yet. Synthesize failed.");
					}
				}
				return;
			}
			else
			{// 标志synthesizing为假,是第一次单击synthesize按钮
				synthesizing=true; //设置synthesizing标志
				stopButton.setEnabled(true);
				playButton.setEnabled(false);
				loopButton.setEnabled(false);
			}
		}
	}//actionPerformed()方法结束
}//SoundApplet类定义结束

/** 定义载入音频文件的线程 */
class SoundFileLoadThread extends Thread
{ 
	JApplet applet;//创建该类对象的JApplet名
	SoundTable soundTable;//哈希表类的实例
	URL soundFileBaseURL; //音频文件基地址
	String soundFileName; //音频文件名
	
	/** 线程类的构造方法 */
	public SoundFileLoadThread(JApplet applet,SoundTable soundTable,URL soundFileBaseURL,String soundFileName)
	{//构造方法
		this.applet=applet;
		this.soundTable=soundTable;
		this.soundFileBaseURL=soundFileBaseURL;
		this.soundFileName=soundFileName;
		setPriority(MIN_PRIORITY);
	}//构造方法结束
	
	/** 线程类的run()方法 */
	public void run()
	{
		AudioClip audioClip=applet.getAudioClip(soundFileBaseURL,soundFileName);//下载音频文件
		soundTable.putClip(audioClip,soundFileName);
			//将“音频文件名--- AudioClip对象”对存入哈希表
	}//run()方法结束
}//SoundFileLoadThread类结束

/** 定义一哈希表类的子类 */
class SoundTable extends java.util.Hashtable
{
	
	/** 构造方法用来设置哈希表的初始容量 */
	public SoundTable(int initialCapacity)
	{
		super(initialCapacity);
	}
	
	/** getClip()方法由文件名获得相应AudioClip对象 */
	public AudioClip getClip(String audioFileName)
	{
		return (AudioClip)get(audioFileName);
	}
	
	/** putClip()方法将“音频文件名--- AudioClip对象”对存入哈希表,实现二者的对应关系 */
	public void putClip(AudioClip clip,String audioFileName)
	{
		put(audioFileName,clip);
	}
}//SoundTable类结束

⌨️ 快捷键说明

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