gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:21:25 24 4
gpt4 key购买 nike

我正在尝试使用此处另一个问题的答案来播放 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com