Conflicting Jar Methods

I am trying to make my first Java GUI music player. So far, I could play MP3s with Javasound and MP3SPI. Now, I want to support .m4a songs, and from what I explored, the best library for this is JAAD. I downloaded it, added it to my project, and it worked great when I tried to play the .m4a song. The problem occurs when I try to play .mp3 after adding the JAAD library. My code for playing songs:

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

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

 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) 

In my opinion, it seems that getAudioInputStream from Javasound and JAAD are contradictory. How to resolve this conflict?

0
source share
1 answer

Well, I found a solution for using MP3SPI and JAAD next to each other based on the berry150 code in the answer here: JAAD terminates other providers . First, you must order banks on the way to classes so that JLayer, MP3SPI, and Tritonous Share load before JAAD. Then, to get an 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 if the mp3 sound, getAudioStreamMethod () from Javasound will be called first since its JAR was loaded first. If the audio is .m4a, a new instance of ACCAudioFileReader () is created and the getAudioInputStream () of the JAAD library is called.

+1
source

Source: https://habr.com/ru/post/1502037/


All Articles