public TestMusicPlay(){ setSize(500, 300); setVisible(true); setLayout(new BorderLayout()); Container c=getContentPane(); JPanel jp=new JPanel(); JButton choose=new JButton("選擇音樂文件"); JButton play=new JButton("播放"); JTextField musictext=new JTextField(20); c.add(jp, BorderLayout.NORTH); jp.add(musictext); jp.add(choose); jp.add(play); choose.addActionListener(new ActionListener() { JFileChooser filechooser=new JFileChooser(); { FileNameExtensionFilter fileExtension=new FileNameExtensionFilter("音頻文件(mp3)", "mp3","wav","mid","au"); filechooser.setFileFilter(fileExtension); } @Override public void actionPerformed(ActionEvent e) { // TODO 自動生成的方法存根 int i=filechooser.showOpenDialog(c); if(i==filechooser.APPROVE_OPTION){ file=filechooser.getSelectedFile(); musictext.setText(file.getAbsolutePath()); } } }); play.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO 自動生成的方法存根 if(file!=null){ try {// if(audioplay!=null)// audioplay.stop(); audioplay=Applet.newAudioClip(file.toURI().toURL()); audioplay.play(); } catch (MalformedURLException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } } }); }這個程序我要通過選擇音樂文件和AudioClip類來實(shí)現(xiàn)音樂播放,可為什么卻觸發(fā)不了?
為什么播放不了音樂?
淡泊如風(fēng)
2016-10-26 23:59:25