LineUnavailableException
Learn what LineUnavailableException is in Java, why it occurs, and how to fix it. This beginner-friendly guide includes practical examples, audio handling tips, and exception handling best practices.
This reference explains LineUnavailableException through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
Basic Java Sound Setup
Before diving into the exception, let’s understand the basic components involved in audio playback:
- Clip – Used to play short audio files
- AudioFormat – Describes the format of the audio (sample rate, encoding, channels, etc.)
- DataLine.Info – Describes the desired line’s capabilities
- AudioSystem.getLine() – Fetches a line matching the specified info
Example 1: Basic Clip Playback (Successful)
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
public class AudioExample {
public static void main(String[] args) {
try {
File soundFile = new File("cherry.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
System.out.println("Playing audio...");
Thread.sleep(3000); // Let it play for a few seconds
clip.close();
audioIn.close();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException | InterruptedException e) {
e.printStackTrace();
}
}
}
Output
Playing audio...
Explanation
Java · example discussed here
import javax.sound.sampled.*;
public class LineUnavailableErrorExample {
public static void main(String[] args) {
try {
AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, true);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
// Try to open the same line twice to cause conflict
line.open(format);
line.open(format); // Likely to throw LineUnavailableException
} catch (LineUnavailableException e) {
System.out.println("Caught LineUnavailableException: " + e.getMessage());
}
}
}
In the above example, we try to open the same audio line twice. The second call fails because the line is already in use. Java throws a LineUnavailableException to prevent a resource conflict.
Example 3: Safe Audio Line Handling with try-catch
import javax.sound.sampled.*;
public class SafeAudioLine {
public static void main(String[] args) {
AudioFormat format = new AudioFormat(22050.0f, 8, 1, true, true);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try (SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) {
line.open(format);
line.start();
byte[] buffer = new byte[1024];
for (int i = 0; i < buffer.length; i++) {
buffer[i] = (byte) (Math.sin(i * 0.1) * 127);
}
line.write(buffer, 0, buffer.length);
line.drain();
} catch (LineUnavailableException e) {
System.out.println("Audio line is unavailable: " + e.getMessage());
}
}
}
Output
Audio line is unavailable: [system-specific message]
Common Mistakes and Fixes
| Mistake | Fix |
|---|---|
| Not closing audio lines | Use line.close() or try-with-resources |
| Requesting unsupported audio formats | Check AudioSystem.isLineSupported(info) |
Not handling LineUnavailableException | Wrap in try-catch |
Recap
LineUnavailableExceptionis thrown when Java can't allocate a requested audio line- It’s part of the
javax.sound.sampledpackage and is a checked exception - Use
try-catchand check for line support before attempting to open audio resources - Always close audio lines to free system resources
Conclusion
LineUnavailableException may seem intimidating at first, but it simply protects your app from misusing or overloading system audio resources. With careful audio line handling, you can build robust Java applications that produce sound as smoothly as a ripe cherry falling from a tree.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.