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:
import sun.audio.*;
import java.io.*;
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);
musicPlayer.start(loop);
} 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() {
initComponents();
Sound sound = new Sound();
sound.music();
}
My problem is that the music doesn't play. Error: java.io.IOException: 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.
My guess is that the
wav
file has been encoded in a format theAudioStream
class doesn't understand. I couldn't find the docs for the class (??) but I would try another file that isn'tMicrosoft 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 theAudioStream
.