Audio playback issue with javasound Stackoverflow example tag

Reading about earlier today I tried to implement the simple code we can see there (with some minor changes like using File

instead URL

):

File file = new File(System.getProperty("user.dir") + "/sound.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
clip.open(ais); // exception

      

But I get java.lang.IllegalArgumentException: Invalid format

when I try to open AudioInputStream

which we see there.

However, when I try it with the following code I got from the internet

File file = new File(System.getProperty("user.dir") + "/sound.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
AudioFormat format = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(ais);
clip.start();

      

works does : sound.wav

reproduces correctly, however i find it awkward for something that should be as simple as our first example given in...

After reading the documentation from the clip , I have confirmed what IllegalArgumentException

is called when [...] the stream audio format is not fully specified or invalid

. However, it is indeed a valid format.

What's wrong with our first example here? AudioSystem.getAudioInputStream () takes an argument File

like, and as you can see from the second example, it does indeed have a valid audio data file because it works. Am I missing something? And, if so, should not point to tag

+3


source to share


1 answer


After some research, it turned out that this may have something to do with the properties of the system. In these properties, you can specify default values ​​for the getLine () methods (getLine (), getClip (), getSourceDataLine (), and getTargetDataLine ()). If you call one of these methods, Java does the following ( AudioSystem description ):

The system property javax.sound.sampled.Clip is set to

javax.sound.sampled.Clip = great.Mixer # great.clip.Clap

The Mixer class is called great.Mixer and the clip is called great.clip.Clap

When you request a clip, Java checks for the following:



  • If Mixer great.Mixer is found and contains Clip great.clip.Clap then return this clip
  • If Mixer great.Mixer is found but does not contain Clip great.clip.Clap then return the first clip listed in Mixer
  • If Mixer great.Mixer contains no clips, or if it is not found, return the first instance of great.clip.Clap file in any Mixer
  • If no Mixer contains a great.clip.Clap file, return the first clip of the first Mixer found
  • If no clip is specified in any mixer then throw IllegalArgumentException

This means that if you get an IllegalArgumentException, no clips are installed in any mixers.

FROM

DataLine.Info info = new DataLine.Info(Clip.class, format);

      

you define a new clip. This means that your next call to the getLine () method will return this object and not give you an error.

+2


source







All Articles