Conflicting Jar Methods

谁说我不能喝 提交于 2019-12-13 08:12:21

问题


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() simply read the stream and writes the bytes to the SourceDataLine)

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)

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


回答1:


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);
            }

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.



来源:https://stackoverflow.com/questions/17823542/conflicting-jar-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!