先看我写的代码:
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import javax.swing.JButton;
import javax.swing.JFrame;public class MainFrame {

JFrame mainface = new JFrame("音乐播发器");
JButton play = new JButton("PLAY");
File f = new File("E:/MP3/3289.wma");

public MainFrame(){ this.mainface.setSize(300,300);
this.mainface.add(this.play,BorderLayout.SOUTH); }



public void addListener(){ this.play.addActionListener(new MyActionListener());

}

public void show(){ this.mainface.setVisible(true);
this.addListener(); }


class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) {
System.out.println(f.exists());
try {
AudioClip auu = Applet.newAudioClip(f.toURL());
System.out.println(f.toURL().toString());
auu.loop();
} catch (MalformedURLException e1) {

e1.printStackTrace();
}

}

}

}//main函数入口public class Test {
public static void main(String args []){
MainFrame mf = new MainFrame();
mf.show();
}
}为什么运行后我点了PLAY  播放不了声音呢?
确定是有音乐文件的;
而且也能得到文件;

解决方案 »

  1.   

    Java 播放不了 wma。请自己搜索“Java 播放 音乐”
      

  2.   

    恩明白了   如果我想放MP3或者是WMA格式的该怎么办呢?自己写个转码?或者是用转码器先转成支持java的
      

  3.   

    /** 
    *AePlayWave.java 
    * Created on 4:14:40 PM Feb 20, 2009 
    *@author Quasar063501 
    *@version 0.1 

    */ 
    import java.io.File; 
    import java.io.IOException; 
    import javax.sound.sampled.AudioFormat; 
    import javax.sound.sampled.AudioInputStream; 
    import javax.sound.sampled.AudioSystem; 
    import javax.sound.sampled.DataLine; 
    import javax.sound.sampled.FloatControl; 
    import javax.sound.sampled.LineUnavailableException; 
    import javax.sound.sampled.SourceDataLine; 
    import javax.sound.sampled.UnsupportedAudioFileException; public class AePlayWave extends Thread { private String filename; private Position curPosition; private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb enum Position { 
    LEFT, RIGHT, NORMAL 
    }; public AePlayWave(String wavfile) { 
    filename = wavfile; 
    curPosition = Position.NORMAL; 
    } public AePlayWave(String wavfile, Position p) { 
    filename = wavfile; 
    curPosition = p; 
    } public void run() { File soundFile = new File(filename); 
    if (!soundFile.exists()) { 
    System.err.println("Wave file not found: " + filename); 
    return; 
    } AudioInputStream audioInputStream = null; 
    try { 
    audioInputStream = AudioSystem.getAudioInputStream(soundFile); 
    } catch (UnsupportedAudioFileException e1) { 
    e1.printStackTrace(); 
    return; 
    } catch (IOException e1) { 
    e1.printStackTrace(); 
    return; 
    } AudioFormat format = audioInputStream.getFormat(); 
    SourceDataLine auline = null; 
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); try { 
    auline = (SourceDataLine) AudioSystem.getLine(info); 
    auline.open(format); 
    } catch (LineUnavailableException e) { 
    e.printStackTrace(); 
    return; 
    } catch (Exception e) { 
    e.printStackTrace(); 
    return; 
    } if (auline.isControlSupported(FloatControl.Type.PAN)) { 
    FloatControl pan = (FloatControl) auline 
    .getControl(FloatControl.Type.PAN); 
    if (curPosition == Position.RIGHT) 
    pan.setValue(1.0f); 
    else if (curPosition == Position.LEFT) 
    pan.setValue(-1.0f); 
    } auline.start(); 
    int nBytesRead = 0; 
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; try { 
    while (nBytesRead != -1) { 
    nBytesRead = audioInputStream.read(abData, 0, abData.length); 
    if (nBytesRead >= 0) 
    auline.write(abData, 0, nBytesRead); 

    } catch (IOException e) { 
    e.printStackTrace(); 
    return; 
    } finally { 
    auline.drain(); 
    auline.close(); 
    } } 

    使用上面的如下: 
    /** 
    *TestWav.java 
    * Created on 4:22:18 PM Feb 20, 2009 
    *@author Quasar063501 
    *@version 0.1 

    */ 
    public class TestWav { 
    public static void main(String args[]) { 
    AePlayWave p = new AePlayWave("src/bg.wav"); 
    p.start();