gpt4 book ai didi

java - 冲突的 jar 方法

转载 作者:行者123 更新时间:2023-12-01 14:18:54 26 4
gpt4 key购买 nike

我一直在尝试用 Java 制作我的第一个 GUI 音乐播放器。到目前为止,我已经能够使用 Javasound 和 MP3SPI 播放 MP3。现在,我想支持 .m4a 歌曲,根据我的研究,最好的库是 JAAD。我下载了它,将其添加到我的项目中,当我尝试播放 .m4a 歌曲时,它运行得非常好。当我在添加 JAAD 库后尝试播放 .mp3 时,会出现问题。我播放歌曲的代码是:

File file = new File(pathToSong);
AudioInputStream audioStream= AudioSystem.getAudioInputStream(file); // Obtains an audio input stream of the song
AudioFormat baseFormat = audioStream.getFormat(); //Obtains the audio format of the song in the audio input stream.
decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, //The type of encoding for the audio stream
baseFormat.getSampleRate(), //Sample rate of the audio stream
16, //Number of bits in each sample of a sound that has this format.
baseFormat.getChannels(), //Number of audio channels in this audio stream
baseFormat.getChannels() * 2, //Number of bytes in each frame of the audiostream
baseFormat.getSampleRate(), //Number of frames played or recorded per second, for the sound in the audio stream
false); //Data stored in little-endian order

decodedAudioStream = AudioSystem.getAudioInputStream(decodedFormat, audioStream); //Obtains an audio input stream of the indicated encoding by converting the provided audio input stream.

playSong(); //Play the song

(playSong() 只是读取流并将字节写入 SourceDataLine)

添加 JAAD 库后尝试播放 .mp3 时出现的错误如下:

java.io.IOException: Resetting to invalid mark
at java.io.BufferedInputStream.reset(BufferedInputStream.java:416)
at net.sourceforge.jaad.spi.javasound.AACAudioFileReader.getAudioInputStream(AACAudioFileReader.java:118)
at net.sourceforge.jaad.spi.javasound.AACAudioFileReader.getAudioInputStream(AACAudioFileReader.java:143)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1162)
at Song.run(Song.java:38)

根据我的理解,Javasound 和 JAAD 的 getAudioInputStream 似乎是冲突的。我该如何解决这个冲突?

最佳答案

嗯,我找到了一个基于 berry150 代码的并行使用 MP3SPI 和 JAAD 的解决方案:JAAD stopping other providers from working 。首先,您必须在类路径中对 jar 进行排序,以便 JLayer、MP3SPI 和 Tritonous Share 在 JAAD 之前加载。然后使用以下代码获取 AudioInputStream:

if (getAudioFormat().equals(".mp3")) {
audioStream = AudioSystem.getAudioInputStream(file); // Obtains an audio input stream of the song
}
else if (getAudioFormat().equals(".m4a")){
audioStream = new AACAudioFileReader().getAudioInputStream(file);
}

因此,如果音频是 mp3,则将首先调用 Javasound 的 getAudioStreamMethod(),因为它的 JAR 首先加载。如果音频是.m4a,则创建 ACCAudioFileReader() 的新实例,并调用 JAAD 库的 getAudioInputStream()。

关于java - 冲突的 jar 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17823542/

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