Results 1 to 8 of 8

Thread: LineUnavailableException for Sound in Applet

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Question LineUnavailableException for Sound in Applet

    I'm playing around with the javax.sound.sampled package and I'm having trouble understanding this LineUnavailableException that keeps apperaring for me. Below I've posted the source for the applet.

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.io.*;
    import java.net.*;
    import javax.sound.sampled.*;
    
    public class SamplePlayer extends Applet {
    	String fileName = "restaurant_1.wav";
    	AudioInputStream sample;
    	
    	private URL getURL (String fileName) {
    		URL url = null;
    		try { url = this.getClass().getResource(fileName); }
    		catch (Exception e) { System.out.println("Failed to load the resource"); }
    		return url;
    	}
    	
    	public void init() {
    		try {
    			sample = AudioSystem.getAudioInputStream(getURL(fileName));
    		
    			Clip clip = AudioSystem.getClip();
    		
    			clip.open(sample);
    		
    			clip.start();
    		} catch (MalformedURLException e) { System.out.println("Malformed URL Exception was encountered"); }
    		catch (IOException e) { System.out.println("IO Exception was encountered"); }
    		catch (LineUnavailableException e) { System.out.println("Line Unavailable Exception was encountered"); }
    		catch (UnsupportedAudioFileException e) { System.out.println("Unsupported Audio File Exception was encountered"); }
    		catch (Exception e) { System.out.println("Some Exception was encountered"); }
    	}
    	
    	public void paint (Graphics g) {
    		int y = 1;
    		
    		g.drawString("Sample file: " + fileName, 10, 15 * y++);
    		g.drawString(" " + sample.getFormat().toString(), 10, 15 * y++);
    		g.drawString(" Sampling rate: " + (int)sample.getFormat().getSampleRate(), 10, 15 * y++);
    		g.drawString(" Sample channels: " + sample.getFormat().getChannels(), 10, 15 * y++);
    		g.drawString(" Encoded format: " + sample.getFormat().getEncoding().toString(), 10, 15 * y++);
    		g.drawString(" Sample size: " + sample.getFormat().getSampleSizeInBits() + "-bit", 10, 15 * y++);
    		g.drawString(" Frame size: " + sample.getFormat().getFrameSize(), 10, 15 * y++);
    	}
    }
    The sourse itself compiles fine and the applet runs however I have the console window telling me that the applet encounters the LineUnavailableException. I looked it up on the Java website and the only thing I got from that was that either (1) the WAV file I'm using is corrupt, (2) the sound device is in use by some other program or (3) the source had trouble opening the WAV file. I troubleshooted all three of those and it's still throwing that exception and I'm not entirely too sure what to do about it as this is really the first time I've encountered it. Can someone help out? Thanks in advance.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: LineUnavailableException for Sound in Applet

    I'm not sure if this is the solution but you should close the clip after you're done with it.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: LineUnavailableException for Sound in Applet

    I did a stack trace on the exception and from what it's saying, the format for the samples isn't usable. It's set to PSM_SIGNED and I've trying to figure out how to change the format but I'm not entirely too sure as to how to change it. I'm guessing that I'm probably going to have to use PSM_UNSIGNED.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: LineUnavailableException for Sound in Applet

    Actually without changing anything in your code I tried playing chimes.wav from windows media folder and it played successfully with no errors in the background
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: LineUnavailableException for Sound in Applet

    hmm That's strange indeed... It's still not working. It won't play the sound.

    I ran two different tests. The MIDI playback works absolutely fine. I know they run off of different sound packages but I still find it strange. Also here are the contents of the stack trace that runs on the thrown exception in question:

    javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 22050.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian not supported. at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:506)
    at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1296)
    at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:107)
    at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1077)
    at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1167)
    at SamplePlayer.init(SamplePlayer.java:19)
    at sun.applet.AppletPanel.run(AppletPanel.java:380)
    at java.lang.Thread.run(Thread.java:595)

    The red text is what's concerning me.

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: LineUnavailableException for Sound in Applet

    maybe your sound card cannot play a file in this format!!
    I doubt it but try playing it on a media player
    or maybe the PCM_SIGNED is not supported by Java (you might need a codec to play it)
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: LineUnavailableException for Sound in Applet

    That's the thing that baffles me. I can open the same WAV file in WMP and it plays fine. I swapped out the WAV with an AIFF file and it did the same thing in the applet however WMP was able to load the file and play it.

    EDIT:
    If PCM_SIGNED isn't supported how do I go about changing the output format? I know about the AudioFormat.Encoding class but I'm not sure how to utilize it properly to change the encoding to another format.

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: LineUnavailableException for Sound in Applet

    I'm not really an expert on sounds in Java but I know for sure that you cannot compare your 30 lines of code with WMP. It supports a couple of codecs that your applet .
    You can google it and look a better ways for doing this
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width