-1

I'm opening multiple audio files as instances of Clip by calling the following method for each audio file:

final Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioStream);
final Clip clip = AudioSystem.getClip(mixerInfo[selectedMixerIdx]);
clip.open(audioInputStream);

However, when running this on certain machines I get LineUnavailableException after opening a few files.

I'm new to the Java Sound API, so I might be completely on the wrong path; but is there any way to see the max number of Clips the Mixer can handle?

flaxel
  • 4,173
  • 4
  • 17
  • 30
Cethy
  • 551
  • 6
  • 18

1 Answers1

2

I'm going to venture a guess that the issue about a line being unavailable is due to an attempt reuse a line. AFAIK, you can open as many Clips as you want (assuming you don't run out of memory).

How many can play at the same time is another question. I think it depends a great deal on the individual PC. I think the last time I tried this was several years ago on an older laptop and it could handle something like 30 Clips or half that many SourceDataLines playing at once.

If you are trying to play a sound file as a Clip concurrently (making many copies of the same audio resource), you might be interested in looking at AudioCue. This makes use of a plan where the source data is stored just once in memory, but multiple cursors (each a playing instance) are managed, and the outputs from the cursors are merged into a single SourceDataLine. Some very complex soundscapes can be built with this tool.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • Thanks for your answer, Phil. So is there a limit to the number of Lines a Mixer can support? – Cethy Apr 06 '21 at 18:13
  • The API for Mixer doesn't mention a limit, and does refer to situations where more than one input line is used in addition to an output line. I confess, when I did my experiment, I just invoked the default mixer, and as I said, was running something like 30 lines or so before the performance started to break down. The Mixer is a little bit of a mystery to me. I don't know how to use it to actually mix audio, and instead built my own mixing system that outputs to a SourceDataLine. – Phil Freihofner Apr 07 '21 at 00:56