Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Error playing OGG files

807589Jul 3 2008 — edited Jul 17 2008
Hi,

I've written the following class to play sound files.
package tools;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class SoundPlayer extends Thread {
   private String filename;

   private boolean loop;

   private Clip clip;

   public SoundPlayer(String filename, boolean loop) {
      this.filename = filename;
      this.loop = loop;
   }

   public void run() {
      setName("SoundPlayer");
      try {
         AudioInputStream stream = AudioSystem.getAudioInputStream(ClassLoader.getSystemResource("resources/sounds/" + filename));
         AudioFormat format = stream.getFormat();
         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);
            stream = AudioSystem.getAudioInputStream(format, stream);
         }
         DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(), ((int) stream.getFrameLength() * format.getFrameSize()));
         clip = (Clip) AudioSystem.getLine(info);
         clip.open(stream);
         if (loop) {
            clip.loop(Clip.LOOP_CONTINUOUSLY);
         }
         clip.start();
      } catch (UnsupportedAudioFileException exception) {
         exception.printStackTrace();
      } catch (IOException exception) {
         exception.printStackTrace();
      } catch (LineUnavailableException exception) {
         exception.printStackTrace();
      }
   }

   public void exit() {
      clip.stop();
      clip.close();
   }
}
Now, I want this class to play OGG files. So i added jorbis and related libraries to my project. See: [http://www.javazoom.net/vorbisspi/documents.html|http://www.javazoom.net/vorbisspi/documents.html]

But that does not work. I get the following Exception:
Exception in thread "SoundPlayer" java.lang.IllegalArgumentException: Unsupported conversion: PCM_SIGNED 44100.0 Hz, -2 bit, stereo, 2 bytes/frame, 20000.0 frames/second,  from VORBISENC 44100.0 Hz, unknown bits per sample, stereo, 1 bytes/frame, 20000.0 frames/second,
   at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:955)
   at tools.SoundPlayer.run(SoundPlayer.java:32)
I did not get an idea where the problem is, exspecially why the sample-rate is -2.

Does anyone know a solution?

Greetings,
DeeDee0815

(Excuse my possibly bad english.)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 14 2008
Added on Jul 3 2008
5 comments
777 views