newbie newbie - 1 year ago 87
Java Question

Music Loop in Java

Good day!

I am doing a game and I want it to have a background sound. I created a class for it and I call it on my main. My code is as follows:


public class Sound {

public void music() {

AudioStream backgroundMusic;
AudioData musicData;
AudioPlayer musicPlayer = AudioPlayer.player;
ContinuousAudioDataStream loop = null;
try {
backgroundMusic = new AudioStream(new FileInputStream("chickendance.wav"));
musicData = backgroundMusic.getData();
loop = new ContinuousAudioDataStream(musicData);
} catch (IOException error) { System.out.println(error);

This is my main class where i call it.

public class HangmanLauncher extends javax.swing.JFrame {

public HangmanLauncher() {
Sound sound = new Sound();;

My problem is that the music doesn't play.
Error: could not create audio stream from input stream.
What does it mean? The type of my file is Microsoft Wave Sound Format and its size is 796kb. May I know what I am doing wrong? Your suggestions will be highly appreciated. Thank you in advance.

Answer Source

My guess is that the wav file has been encoded in a format the AudioStream class doesn't understand. I couldn't find the docs for the class (??) but I would try another file that isn't Microsoft Wave Sound. Again, don't know the specifics of that encoding but it being Microsoft it's probably proprietary and therefore not in the Sun implementation of the AudioStream.