冲突的Jar方法 [英] Conflicting Jar Methods

查看:86
本文介绍了冲突的Jar方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to make my first GUI music player in Java. So far I have been able to play MP3 with Javasound and MP3SPI. Now, I want to support .m4a songs and from what I have researched the best library to do this is JAAD. I downloaded it, added it to my project and it worked perfectly when I tried to play a .m4a song. The problem happens when I try to play an .mp3 after adding the JAAD library. My code for playing songs is:

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)

(playSong() simply read the stream and writes the bytes to the SourceDataLine)

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

The error I get when I try to play an .mp3 after adding the JAAD library is the following:

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是冲突的.我该如何解决此冲突?

From my understanding, it seems that the getAudioInputStream from Javasound and from JAAD are conflicting. How can I resolve this conflict?

推荐答案

我在这里的答案中基于berry150的代码找到了一个将MP3SPI和JAAD相互结合使用的解决方案:

Well I found a solution for using MP3SPI and JAAD alongside each other based on berry150's code in the answer here: JAAD stopping other providers from working. First, you have to order the jars in the classpath so that JLayer, MP3SPI and Tritonous Share are loaded before JAAD. Then for getting the AudioInputStream, use the following code:

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().

So what happens is that if the audio is mp3, the getAudioStreamMethod() from Javasound will be called first since its JAR was loaded first. If the audio is .m4a, a new instance of the ACCAudioFileReader() is created and the getAudioInputStream() of the JAAD library is called.

这篇关于冲突的Jar方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆