java制作一个MP3播放器,哪个高手帮忙看看代码
这是MusicPlayer.javapackage com.test.audio;
import java.io.File;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.amenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class MusicPlayer extends Frame{
boolean isStop=ture;
boolean hasStop=ture;
String filepath;
String filename;
AudioInputStream audioInputStream;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
List list;
Lable lablefilepath;
Lable lablefilename;
public MusicPlayer(){
setLayout(new BorderLayout());
setTitle("音乐播放器");
setSize(350,370);
MenuBar menubar=new MenuBar();
Menu menufile=new Menu("文件");
MenuItem menuopen=new MenuItem("打开",new MenuShortcut(KeyEvent.VK-O));
menufile.add(menuopen);
menufile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
open();
}
});
menubar.add(menufile);
setMenuBar(menubar);
list=new list(10);
list.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2){
filename=list.getSelectedItem();
play();
}
}
});
add(list,"Center");
Panel panel=new Panel(new GridLayout(2,1));
labelfilepath=new Lable("播放目录:");
lablefilename=new Lable("播放文件:");
panel.add(labelfilepath);
panel.add(labelfilename);
add(panel,"North");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
private void open(){
FileDialog dialog=new FileDialog(this,"open",0);
dialog.setVisible(true);
filepath=dialog.getDirectory();
if(filepath!=null){
lablefilepath.setText("播放目录:"+filepath);
list.removeAll();
File filedir=new File(filepath);
File[] filelist=filedir.listFiles();
for(File file:filelist){
string filename=file.getName().toLowerCase();
if(filename.endsWith(".mp3")||filename.endsWith(".wav")){
list.add(filename);
}
}
}
}
private void play(){
try{
isStop=true;
System.out.print("开始播放:"+filename);
while(!hasStop){
System.out.print(".");
try{
Thread.sleep(10);
}catch(Exception e){
}
}
System.out.println("");
File file=new File(filepath+filename);
labelfilename.setText("播放文件:"+filename);
audioInputStream=AudioSystem.getAudioInputSream(file);
audioFormat=audioInputStream.getFormat();
if(audioFormat.getEncoding()!=AudioFormat.Encording.PCM_SIGNED){
audioFormat=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,audioFormat.getSampleRate(),16,audioFormat.getChannels(),audioFormat.getChannels()*2,audioFormat.getSampleRate(),false);
audioInputStream=AudioSystem.getAudioInputStream(audioFormat,audioInputStream);
}
DataLine.Info dataLineInfo=new DataLine.Info(sourceDataLine.class,audioFormat,AudioSystem.NOT_SPECIFIED);
sourceDataLine=(SourceDataLine)AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
isStop=false;
Thread playThread=new Thread(new PlayThread());
playThread.star();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
new MusicPlayer();
}
}
这个是PlayThread.java
class PlayThread extends Thread
{
byte tempBuffer[]=new byte[320];
public void run()
{
try
{
int cnt;
hasStop=false;
while((cnt=audioInputStream.read(tempBuffer,0,tempBuffer.length))!=-1)
{
if(isStop)
break;
if(cnt>0)
{
sourceDataLine.write(tempBuffer,0,cnt);
}
sourceDataLine.drain();
sourceDataLine.close();
hasStop=true;
}
catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
}
}
已经下载了J11.0.JAR,MP3SPLI.9.4.JAR ,tritonus_share.jar三个包
下一步应该怎么搞!书上写的不太清楚,我是初学者不是很明白