刚写的播放声音的类:public class SimpleSoundPlayer {
    int samples_size;
    byte[] samples ;
    AudioInputStream inputstream;
    AudioFormat format;
    public SimpleSoundPlayer(String filename) {
        try {
                inputstream = AudioSystem.getAudioInputStream(new File(filename));
                format = inputstream.getFormat();
        } catch (IOException ex1) {
            ex1.printStackTrace();
            }
         catch (UnsupportedAudioFileException ex) {
            ex.printStackTrace();
        }
    }
    public byte[] getSamples(){
        samples_size = (int)(inputstream.getFrameLength()*format.getFrameSize());
        samples = new byte[samples_size];
        DataInputStream ds  = new DataInputStream(inputstream);
        try {
            ds.readFully(samples);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return samples;
    }
    public void play(InputStream is)  {
        int buffer_size = format.getFrameSize()*Math.round(format.getSampleRate()/10);        byte[] buffer = new byte[buffer_size];
        SourceDataLine line;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);
        try {
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(format,buffer_size);
        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
            return;
        }
        line.start();
      try {  int numBytesRead =0;
          while( numBytesRead != -1){
              numBytesRead = is.read(buffer, 0, buffer_size);
              if(numBytesRead != -1){
                  line.write(buffer,0,numBytesRead);
              }
          } 
      }catch (IOException ex1) {
          ex1.printStackTrace();
      }
      line.drain();
      line.close();
  }
}
可是发现这个只能是播放WAV格式的,别的格式的声音要怎么处理呢? 
帮忙啊

解决方案 »

  1.   

    可是发现这个只能是播放WAV格式的,别的格式的声音要怎么处理呢? 
    帮忙啊
      

  2.   

    使用JLayer
    JLayer是一个Java类库用来解码,转换,播放MP3文件。参见http://www.javazoom.net/javalayer/javalayer.html
      

  3.   

    java能处理的音频格式不多,主要是java为了要跨平台不得不选择各个平台都支持的音频格式www.java2code.com看看有没有例子