- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用此处另一个问题的答案来播放 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/
我正在使用 AudioInputStream 在 Java 中进行一些基本的音频处理。我的程序可以正确加载用两个轨道编码的 16 位整数 PCM 音频,但不能用立体声编码。当呈现一个文件时(根据 ff
我正处于用 Java 编写小型音乐/节奏游戏的早期阶段(通过 Slick 框架,它又使用 OpenAL,但这可能与此无关)。游戏需要读取(和播放)多个 WAV 格式的声音文件,但一些文件抛出 [jav
这是我在这里的第一篇文章。我目前正在 Apache-Karaf 3.0.0 上的 Scala 中开发一个简单的 http 音频 servlet。我将它部署为一些 bundle 中的功能,这些 bund
我看到了多个关于流 MP3 流(如 Icecast)的 Stack Overflow 问题。他们都说使用 MP3SPI 库,我就是这样。 MP3SPI 用于支持音频/mpeg mime 类型。这就是我
我在尝试使用 Java 播放声音时遇到问题;如果我使用 AudioInputStream soundIn = AudioSystem.getAudioInputStream(new File("./s
我已经研究了好几天来弄清楚如何解决这个问题,但没有运气。 PlayN JavaSound类使用javax.sound.sampled.AudioSystem类来播放声音,并且似乎不支持mp3格式。 j
我已经成功地通过 TCP 套接字发送和读取文本和图像数据。但是我无法发送和读取音频流数据。 服务器端示例代码: public class ServerAudio { /** * @p
我正在尝试使用我在这里找到的代码在我的 java 程序中加载 wav 文件: How do I get a sound file's total time in Java? 但是,在第 14 行: A
我正在尝试使用此处另一个问题的答案来播放 OGG 音频文件:Playing ogg files in eclipse 使用javazoom library我为我的项目成功下载并配置了该文件,我正在尝试
我的 Java 项目中有两个类,一个是 反弹 import java.awt.*; import java.awt.event.*; import javax.swing.*; public clas
我正在使用 java 6 ,我正在尝试加载 wav 文件,以获取其持续时间如下: public static double getAudioFileDuration(File file) {
我是一名优秀的程序员,十分优秀!