import java.awt.*;
import java.awt.event.*;
import sun.audio.*; 
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class PlayMusic extends JFrame implements ActionListener,ListSelectionListener{
static JButton play=new JButton("播放");
        static JButton stop=new JButton("停止");
static JButton loop=new JButton("循环播放");
static  DefaultListModel data=new DefaultListModel();
static  JList list=new JList(data);
static AudioStream as;
static String[] musicName={"1.wav","2.wav","3.wav","4.wav"};
public static void main(String[] args) throws IOException{
PlayMusic s=new PlayMusic();
for(int i=0;i<musicName.length;i++){
data.addElement(musicName[i]);
}
list.setSelectedIndex(0);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane jscrolling=new JScrollPane(list);
JPanel north=new JPanel(new FlowLayout());
north.add(play);
north.add(stop);
north.add(loop);
JPanel center=new JPanel(new GridLayout(1,1));
center.add(jscrolling);
JPanel all=new JPanel(new BorderLayout());
all.add("North",north);
all.add("Center",center);
s.add(all);
play.addActionListener(s);
stop.addActionListener(s);
loop.addActionListener(s);
list.addListSelectionListener(s);
s.setSize(300,400);
s.validate();
s.pack();
s.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
Object e=arg0.getSource();
if(e==play)
AudioPlayer.player.start (as); 
else if(e==stop)
AudioPlayer.player.stop (as); 
}
static InputStream creatStream()throws IOException{
InputStream in=new FileInputStream(list.getSelectedValue().toString());
return in;
}
public void valueChanged(ListSelectionEvent arg0){
if(!arg0.getValueIsAdjusting())
try {
InputStream out= new FileInputStream(list.getSelectedValues().toString());
as=new AudioStream(out);
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}
}
每次运行点击框里的列表时都提示如下异常,实在是自己无法解决,相关的音乐文件我放在与项目相关的文件夹里,但为什么会这样呢,请高人指点指点。
java.io.FileNotFoundException: [Ljava.lang.Object;@1d1e730 (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at PlayMusic.valueChanged(PlayMusic.java:63)
at javax.swing.JList.fireSelectionValueChanged(Unknown Source)

解决方案 »

  1.   

    很明显是你的 音乐文件找不到.        如果你是eclipse下面直接建的工程的话. 要放到bin 里面.自己手写的话..最好把音乐路径的   全路径   也就是E:\yourname\yourname 整个路径全加上.
          就肯定不会出错了. 
      

  2.   

    InputStream out= new FileInputStream(list.getSelectedValues().toString()); 改为:InputStream out= new FileInputStream(list.getSelectedValue().toString()); 注:即就是将getSelectedValues -> getSelectedValue,另外,还不能打开的时候将相对路径x.wav换为绝对路径就行了