RT    
能不能实现播放mp3文件?用什么函数?高手给指点一下。先谢了……

解决方案 »

  1.   

    这里http://java.ccidnet.com/art/5179/20060317/482627_1.html有个现成可以下载.你研究下吧
      

  2.   

    如果音频数据是压缩格式的,如MP3或Ogg,必须先进行一次转换——把MP3/Ogg解码成PCM。解码主要包括三个步骤:   1、创建一个解压缩结果的定制AudioFormat(PCM编码),但保留和原压缩流一样的取样率、通道信息等。   2、创建一个AudioInputStream把原来的AudioInputStream转换成新的AudioFormat格式。   3、获得一个处理解码后格式的SourceDataLine。   如下所示: AudioFormat decodedFormat = new AudioFormat(
    AudioFormat.Encoding.PCM_SIGNED,
    baseFormat.getSampleRate(),
    16,
    baseFormat.getChannels(),
    baseFormat.getChannels() * 2,
    baseFormat.getSampleRate(),
    false);
    ais = AudioSystem.getAudioInputStream(decodedFormat, ais);
    line = getLine(decodedFormat);   getLine()方法的返回值是一个与参数中指定的AudioFormat兼容的SourceDataLine。如果不能获得兼容的SourceDataLine,getLine()返回null。在getLine()方法中,我们首先创建和填充一个DataLine.Info结构,调用AudioSystem.getLine()方法,将info结构传递给AudioSystem类工厂。 private SourceDataLine getLine(AudioFormat audioFormat) {
    SourceDataLine res = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
    audioFormat);
    try {
    res = (SourceDataLine) AudioSystem.getLine(info);
    res.open(audioFormat);
    }
    catch (Exception e) {
    }
    return res;

      准备好AudioInputStream和SourceDataLine之后,playMusic()剩余的任务已经很简单:用一个循环从AudioInputStream读取数据,然后写入到SourceDataLine。 int inBytes = 0;
    while ((inBytes != -1) && (!stopped) && (!threadExit)) {
    try {
    inBytes = ais.read(audioData, 0, BUFFER_SIZE);
    }
    catch (IOException e) { e.printStackTrace(); }
    if (inBytes >= 0) {
    int outBytes = line.write(audioData, 0, inBytes);
    }
    if (paused) waitforSignal();
    }