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

标签 java audio ogg

我正在尝试使用此处另一个问题的答案来播放 OGG 音频文件:Playing ogg files in eclipse

使用javazoom library我为我的项目成功下载并配置了该文件,我正在尝试使用 xav的应答代码:

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。

关于javax.sound.sampled.UnsupportedAudioFileException 使用 javazoom 播放 OGG 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37502957/

相关文章:

java - 如何在java中播放声音?

java - 应用程序崩溃时编辑对话框

java - 获取两个 java.util.Date 的平均值

java - 如何实例化一个通用集合数组?

javascript - 从浏览器中的麦克风获取音频输入并提取特征

html - 在网页中插入音频文件

java - ArrayList<Integer> 是否允许添加字符串?

java - 处理 3 中的音频输入流

c# - 使用 .NET 检测 .ogg 文件属性? channel 数,每 channel 位数,采样率?

php - ffmpeg/PHP - 将任何视频格式转换为 ogg 时出现问题 - 断断续续的视频/无音频 - win64