Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5021234

Java does not play particular .au files

    XMLWordPrintable

    Details

      Description

      Java does not play a particular .au file, while other tools on Solaris do playing it.

      Testcase
      --------
      The original wakeup.au is attached to this bugreport.

      # /usr/bin/audioplay wakeup.au
      => You hear the sound.

      # /usr/dt/bin/sdtaudio wakeup.au
      => You hear the sound.

      Now, try to play it with Java. Use the following source and compile it.

      // begin source
      // test case from the Java Developer Almanach, Addison Wesley
      // http://javaalmanac.com/egs/javax.sound.sampled/Load.html

      import java.io.*;
      import javax.sound.sampled.*;

      public class Jplay {

        public static void main(String[] args) {
            if (args.length < 1) {
              System.out.println("java Jplay audiofile");
              System.exit(0);
            }
            new Jplay(args[0]);
        }

        public void printFormat(AudioFormat format) {
            System.out.println("Encoding: "+format.getEncoding());
            System.out.println("SampleRate: "+format.getSampleRate());
            System.out.println("SampleSizeInBits: "+format.getSampleSizeInBits());
            System.out.println("Channels: "+format.getChannels());
            System.out.println("FrameSize: "+format.getFrameSize());
            System.out.println("FrameRate: "+format.getFrameRate());
        }

        public Jplay(String sound) {
          try {

            AudioInputStream stream = AudioSystem.getAudioInputStream(new File(sound));
          
            // At present, ALAW and ULAW encodings must be converted
            // to PCM_SIGNED before it can be played
            AudioFormat format = stream.getFormat();
            System.out.println("Stream:");
            printFormat(format);

            if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
                format = new AudioFormat(
                         AudioFormat.Encoding.PCM_SIGNED,
                         format.getSampleRate(),
                         format.getSampleSizeInBits()*2,
                         format.getChannels(),
                         format.getFrameSize()*2,
                         format.getFrameRate(),
                         true); // big endian
                 stream = AudioSystem.getAudioInputStream(format, stream);
                 System.out.println("\nConverted stream:");
                 printFormat(format);
            }
          
            DataLine.Info info = new DataLine.Info(
               Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize()));
            Clip clip = (Clip) AudioSystem.getLine(info);

            // Add a listener for line events
            clip.addLineListener(new LineListener() {
              public void update(LineEvent evt) {
                  if (evt.getType() == LineEvent.Type.STOP) {
                     System.out.println("Sound stopped.");
                     System.exit(0);
                  }
              }
            });

            System.out.println("Playing sound...");
            clip.open(stream);
            clip.start();
            
         } catch (IOException e) {
           System.err.println(e);
         } catch (LineUnavailableException e) {
           System.err.println(e);
         } catch (UnsupportedAudioFileException e) {
           System.err.println(e);
         }
        }
      }
      // end source

      # java -version
      java version "1.4.2_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
      Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)

      (I've also tried build 1.5.0-beta2-b44, but I got the same results)

      # java Jplay wakeup.au
      Stream:
      Encoding: ULAW
      SampleRate: 8000.0
      SampleSizeInBits: 8
      Channels: 1
      FrameSize: 1
      FrameRate: 8000.0

      Converted stream:
      Encoding: PCM_SIGNED
      SampleRate: 8000.0
      SampleSizeInBits: 16
      Channels: 1
      FrameSize: 2
      FrameRate: 8000.0
      Exception in thread "main" java.lang.IllegalArgumentException: No line matching interface Clip supporting format PCM_SIGNED, 8000.0 Hz, 16 bit, mono, big-endian, audio data, and buffers of -2 to -2 bytes is supported.
              at javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:339)
              at Jplay.<init>(Jplay.java:50)
              at Jplay.main(Jplay.java:11)

      => You don't hear the sound. The original .au can not be played by using Java, but it can be played using other tools. In Java you get an IllegalArgumentException.

        Attachments

          Activity

            People

            Assignee:
            fbomerssunw Florian Bomers (Inactive)
            Reporter:
            jloefflm Johann Löfflmann (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: