java制作的applet小型播放器

时间:2022-03-06 15:14:58
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AudioChoiceTest extends Applet implements ActionListener
{
//程序中声音名字
public final String[] AUDIO={"chengyixun","lengku","wangluodang"};
//包含上述字符串数组的下拉框
private Choice choice;
private AudioClip[] clips;
//控制播放停止的按钮
private Button playClip;
private Button loopClip;
private Button stopClip;
private Button stopAllClips;

//跟踪当前哪些音频在播放
private boolean[] clipsPlaying;

public void init()
{
setBackground(
new Color(48,255,0));

//构建下拉框和AudioClip对象
choice=new Choice();
clips
=new AudioClip[AUDIO.length];
clipsPlaying
=new boolean[AUDIO.length];
for(int i=0;i<AUDIO.length;i++)
{
choice.add(AUDIO[i]);
clips[i]
=getAudioClip(getCodeBase(),"audio/"+AUDIO[i]+".MID");
//标志这个音乐是否在播放
clipsPlaying[i]=false;

}
add(choice);
playClip
=new Button("Play clip");
playClip.addActionListener(
this);
add(playClip);
loopClip
=new Button("Loop clip");
loopClip.addActionListener(
this);
add(loopClip);
stopClip
=new Button("Stop clip");
stopClip.addActionListener(
this);
add(stopClip);

stopAllClips
=new Button("Stop all clips");
stopAllClips.addActionListener(
this);
add(stopAllClips);

//如果没什么要停止的话把按钮变灰
stopClip.setEnabled(false);
stopAllClips.setEnabled(
false);
}

//停止所有音频的播放
//Overrides: stop() in Applet
/*Called by the browser or applet viewer to inform this applet that it should stop its execution.
It is called when the Web page that contains this applet has been replaced by another page, and
also just before the applet is to be destroyed.
*/
//不要stop这个函数也没关系 public void stop()
{
for(int i=0;i<AUDIO.length;i++)
{
if(clipsPlaying[i])
clips[i].stop();
}
}


public void actionPerformed(ActionEvent e)
{
int clipIndex=choice.getSelectedIndex();
AudioClip clip
=clips[clipIndex];
if(e.getSource()==playClip)
{
clip.play();
stopClip.setEnabled(
true);
stopAllClips.setEnabled(
true);
clipsPlaying[clipIndex]
=true;
}
else if(e.getSource()==loopClip)
{
clip.loop();
stopClip.setEnabled(
true);
stopAllClips.setEnabled(
true);
clipsPlaying[clipIndex]
=true;

}
else if(e.getSource()==stopClip)
{
clip.stop();
stopClip.setEnabled(
false);
stopAllClips.setEnabled(
false);
clipsPlaying[clipIndex]
=false;




//只要有一个音频片段在播放就激活停止按钮
for(int i=0;i<AUDIO.length;i++)
{
if(clipsPlaying[i])
{
stopClip.setEnabled(
true);
stopAllClips.setEnabled(
true);
break;
}


}
}
//end else if
else if(e.getSource()==stopAllClips)
{
for(int i=0;i<AUDIO.length;i++)
{
if(clipsPlaying[i])
{
clips[i].stop();
clipsPlaying[i]
=false;

}

}
stopClip.setEnabled(
false);
stopAllClips.setEnabled(
false);
}

}
// actionPerformed


}

java制作的applet小型播放器