javax.sound.sampled.UnsupportedAudioFileException使用javazoom播放OGG文件

javax.sound.sampled.UnsupportedAudioFileException使用javazoom播放OGG文件,java,audio,ogg,Java,Audio,Ogg,我正在尝试播放OGG音频文件,请回答下面的另一个问题: 使用已成功下载并为我的项目配置的,我正在尝试使用的回答代码: import java.io.File; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import j

我正在尝试播放OGG音频文件,请回答下面的另一个问题:

使用已成功下载并为我的项目配置的,我正在尝试使用的回答代码:

import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javazoom.spi.PropertiesContainer;

public class OGGPlayer {
public final String fileName;

public boolean mustStop = false;


public OGGPlayer(String pFileName) {
    fileName = pFileName;
}

public void play() throws Exception {
    mustStop = false;
    File file = new File(fileName);
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
    if (audioInputStream == null) {
        throw new Exception("Unable to get audio input stream");
    }
    AudioFormat baseFormat = audioInputStream.getFormat();
    AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
        baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
        baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
    AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(decodedFormat,
        audioInputStream);
    if (!(decodedAudioInputStream instanceof PropertiesContainer)) {
        throw new Exception("Wrong PropertiesContainer instance");
    }

    DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, decodedFormat);
    SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
    sourceDataLine.open(decodedFormat);

    byte[] tempBuffer = new byte[4096];

    // Start
    sourceDataLine.start();
    int nbReadBytes = 0;
    while (nbReadBytes != -1) {
        if (mustStop) {
            break;
        }
        nbReadBytes = decodedAudioInputStream.read(tempBuffer, 0, tempBuffer.length);
        if (nbReadBytes != -1)
            sourceDataLine.write(tempBuffer, 0, nbReadBytes);
    }

    // Stop
    sourceDataLine.drain();
    sourceDataLine.stop();
    sourceDataLine.close();
    decodedAudioInputStream.close();
    audioInputStream.close();
}

public void setMustStop(boolean pMustStop) {
    mustStop = pMustStop;
}

public void stop() {
    mustStop = true;
}

}
代码对我来说很有意义,但我在简单地使用它时遇到了困难。我似乎无法让类读取我的OGG文件,为了实现这个功能,我以各种方式对其进行了修改。我在同一个项目中读取WAV文件,使用

AudioInputStream audioIn = AudioSystem.getAudioInputStream(Thread.currentThread().getContextClassLoader().getResource("res/audio/" + fileName + ".wav"));
我尝试过这种方法的变体,使用File类,将读取作为资源,我可以在项目中成功地找到我的OGG文件,但它一直抛出异常

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL
这是我在这个项目中第四次或第五次尝试让音频播放非WAV音频。我试着用MediaPlayer和其他一些方法播放MP3,这是我尝试播放OGG文件的第二种方法。我正在尝试播放大而长的音乐曲目作为背景,但由于文件大小的原因,我无法像播放其他WAV音效那样运行它们


非常感谢您在播放这些OGG文件时提供的任何帮助。我觉得这次尝试离我的目标很近,它看起来简单而可靠,但它只是拒绝正确读取我的文件。提前感谢您的帮助。

这里有一种方法可以读取mp3和ogg文件,它可以播放vorbis格式的文件,但不能播放flac格式的文件;因此,您必须在此处进行检查-并非所有以.ogg结尾的文件都受支持:

import javazoom.spi.vorbis.sampled.file.VorbisAudioFileReader;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
import org.tritonus.share.sampled.file.TAudioFileReader;
import org.tritonus.sampled.file.AuAudioFileReader;

  AudioInputStream createOggMp3(File fileIn) throws IOException, Exception {
    AudioInputStream audioInputStream=null;
    AudioFormat targetFormat=null;
    try {
      AudioInputStream in=null;
      if(fileIn.getName().endsWith(".ogg")) {
        VorbisAudioFileReader vb=new VorbisAudioFileReader();
        in=vb.getAudioInputStream(fileIn);
      }
      else if(fileIn.getName().endsWith(".mp3")) {
        MpegAudioFileReader mp=new MpegAudioFileReader();
        in=mp.getAudioInputStream(fileIn);
      }
      AudioFormat baseFormat=in.getFormat();
      targetFormat=new AudioFormat(
              AudioFormat.Encoding.PCM_SIGNED,
              baseFormat.getSampleRate(),
              16,
              baseFormat.getChannels(),
              baseFormat.getChannels() * 2,
              baseFormat.getSampleRate(),
              false);
      audioInputStream=AudioSystem.getAudioInputStream(targetFormat, in);
    }
    catch(UnsupportedAudioFileException ue) { System.out.println("\nUnsupported Audio"); }
    return audioInputStream;
  }
它返回一个音频输入流,您可以使用该流,如下所示:

if(fileIn.getName().endsWith(".ogg") || fileIn.getName().endsWith(".mp3")) {
  audioInputStream=createOggMp3(fileIn);
}
else { // wav
    audioInputStream=AudioSystem.getAudioInputStream(fileIn);
  }
您的解码音频格式是

decodedFormat=audioInputStream.getFormat();

然后您可以继续使用SourceDataline。

JavaZoom站点上的mp3spi代码读取MP3。您需要来自JavaZoom站点的vorbisspi和相关的JOrbis代码。谢谢。我对自己非常失望。这是一个非常明显的情况,工作太晚,非常愚蠢和愚蠢。谢谢你的帮助。