?? soundplayer.java
字號:
//SoundPlayer.java
import sun.audio.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class SoundPlayer extends Frame
implements FilenameFilter,ActionListener
{
Button openButton = new Button("Open");
Button playButton = new Button("Play");
Button loopButton = new Button("Loop");
Button stopButton = new Button("Stop");
Label filename = new Label(" ");
File theFile = null;
AudioData theData = null;
InputStream nowPlaying = null;
public SoundPlayer(){
//設置標題欄文字
super("SoundPlayer");
{ System.exit(0);
}
}
);
//設置窗口大小
setSize(300,200);
//設置窗口背景色
setBackground(Color.orange);
//設置窗口上方面板控件內容
Panel north = new Panel();
north.setLayout(new FlowLayout(FlowLayout.LEFT));
north.add(new Label("File: "));
north.add("NORTH",filename);
add("North",north);
//設置窗口下方面板控件內容
Panel south = new Panel();
south.add(openButton);
south.add(playButton);
south.add(loopButton);
south.add(stopButton);
add("South",south);
//為按鈕添加事件監聽
openButton.addActionListener(this);
playButton.addActionListener(this);
loopButton.addActionListener(this);
stopButton.addActionListener(this);
}
//主函數,應用程序入口處
public static void main(String[] args){
SoundPlayer sp = new SoundPlayer();
sp.show();
}
//打開文件
public void open(){
FileDialog fd = new FileDialog(this,"please select a file");
fd.setFilenameFilter(this);
fd.show();
try{
theFile = new File(fd.getDirectory()+"/"+fd.getFile());
if(theFile != null){
filename.setText(theFile.getName());
FileInputStream fis = new FileInputStream(theFile);
AudioStream as = new AudioStream(fis);
theData = as.getData();
}
}
catch(IOException e){
System.err.println(e);
}
catch(SecurityException e){
System.out.println("security exception");
}
}
//開始播放
public void play(){
stop();
if(theData == null) open();
if(theData != null){
AudioDataStream ads = new AudioDataStream(theData);
AudioPlayer.player.start(ads);
nowPlaying = ads;
}
}
//停止播放
public void stop(){
if(nowPlaying != null){
AudioPlayer.player.stop(nowPlaying);
nowPlaying = null;
}
}
//循環播放
public void loop(){
stop();
if(theData == null) open();
if(theData != null){
ContinuousAudioDataStream cads
= new ContinuousAudioDataStream(theData);
AudioPlayer.player.start(cads);
nowPlaying = cads;
}
}
//事件響應
public void actionPerformed(ActionEvent e){
if(e.getSource() == playButton){
play();
}
if(e.getSource() == openButton){
open();
}
if(e.getSource() == loopButton){
loop();
}
if(e.getSource() == stopButton){
stop();
}
}
public boolean accept(File dir,String name){
return true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -