participate


Java Sound - LineUnavailableException
<<   Back to Forum  |   Give us Feedback
This topic has 2 replies on 1 page.
waflik
Posts:1
Registered: 6/27/07
LineUnavailableException   
Jun 27, 2007 11:25 AM

 
Help me, please! If I run this code:
import java.util.Random;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class test {
 
  public static void main(String[] args) {
    try {
      AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
      SourceDataLine	line = null;
      DataLine.Info	info = new DataLine.Info(SourceDataLine.class, format);
      try
		{
			line = (SourceDataLine) AudioSystem.getLine(info);
		}
		catch (LineUnavailableException e)
		{
			e.printStackTrace();
			System.exit(1);
		}
      line.open(format);
      line.start();
      
      int velikost = line.getBufferSize();
      byte[] data = new byte[velikost];
      Random nahodne = new Random();
      long start = System.currentTimeMillis();
      for (int i = 0; i < 10; i++) {
        nahodne.nextBytes(data);
        line.write(data, 0, velikost);
      }
      
      line.drain();
      line.close();
    } catch (LineUnavailableException e) {
    System.setErr(System.out);
      e.printStackTrace();
    }
 
  }
}

I got this error mesage:
javax.sound.sampled.LineUnavailableException: line with format PCM_UNSIGNED 44100.0 Hz, 8 bit, mono, 1 bytes/frame, not supported.
at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(Unknown Source)
at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
at test.main(test.java:24)
What I should to do?
 
finn_hippie
Posts:2
Registered: 5/26/07
Re: LineUnavailableException   
Jul 1, 2007 5:00 PM (reply 1 of 2)  (In reply to original post )

 
Have you tried to open your line as signed? Your system may just not offer unsigned line.
 
SlugFiller
Posts:40
Registered: 4/20/07
Re: LineUnavailableException   
Apr 28, 2008 5:57 PM (reply 2 of 2)  (In reply to original post )

 
As I've recently solved a similar problem, I can tell you exactly what's wrong and how to fix it.

The problem is with AudioSystem.getLine. It returns the first line(in any mixer) that reports it can support your format. Unfortunately, a line may report as supporting a format, but still fail to open, giving LineUnavailableException, roughly saying the format is unsupported.

The only solution is to enumerate matching lines from all mixers, and try opening them. The following code may be useful to that effect:
	private SourceDataLine getSourceDataLine(AudioFormat format) {
		try {
			DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
			for (Mixer.Info mi : AudioSystem.getMixerInfo()) {
				SourceDataLine dataline = null;
				try {
					Mixer mixer = AudioSystem.getMixer(mi);
					dataline = (SourceDataLine)mixer.getLine(info);
					dataline.open(format);
					dataline.start();
					return dataline;
				}
				catch (Exception e) {}
				if (dataline != null)
					try {
						dataline.close();
					}
					catch (Exception e) {}
			}
		}
		catch (Exception e) {}
		return null;
	}

The above function iterates the available mixers, trying to open and start each individual line returned by each mixer. If no mixer supports the format, it returns null.

Common pitfall: I also tried enumerating mixer lines, and using AudioSystem.getLine with the detailed info object. This did return a working line, but one which was skippy, probably due to real-time format conversion. Using mixer.getLine with the generic info works best.
 
This topic has 2 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 25
  • Guests : 132

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums