1
I create a class to create the Audioplayer
package Audio;
import javax.sound.sampled.*;
public class AudioPlayer {
private Clip clip;
public AudioPlayer(String s) {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(s));
AudioFormat baseFormat = ais.getFormat();
AudioFormat decodeFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false
);
AudioInputStream dais =
AudioSystem.getAudioInputStream(
decodeFormat, ais);
clip = AudioSystem.getClip();
clip.open(dais);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void play() {
if(clip == null) return;
stop();
clip.setFramePosition(0);
clip.start();
}
public void stop() {
if(clip.isRunning()) clip.stop();
}
public void close() {
stop();
clip.close();
}
}
then I try to put on the menu of my game (Gamestate):
[...]
import Audio.AudioPlayer;
[...]
private AudioPlayer bgMusic;
[...]
bgMusic = new AudioPlayer("Resources/Musics/menu.mp3");
bgMusic.play();
And then I get that mistake:
java.lang.NullPointerException
at javazoom.spi.mpeg.sampled.file.MpegAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at Audio.AudioPlayer.<init>(AudioPlayer.java:13)
at GameState.MenuState.<init>(MenuState.java:38)
at GameState.GameStateManager.loadState(GameStateManager.java:30)
at GameState.GameStateManager.<init>(GameStateManager.java:24)
at main.GamePanel.init(GamePanel.java:64)
at main.GamePanel.run(GamePanel.java:70)
at java.lang.Thread.run(Unknown Source)
Can someone help me???
EDIT:
I had the JDK 7, I installed the 8, because of the Javafx, but I have a problem in the 8... this is another story...
But, I got this Class(Audioplayer) from this video:
And he used an MP3 song, so I’m kind of doubtful....
Hi, I tried so: I copied the MP3 to Local Disk, and tried to open it like this: 'bgMusic = new Audioplayer("C:/menu.mp3");' but it gave the same error, and, I’m using Eclipse. Vlw
– Tiago Porsch
Updated, from a look.
– Josh
I updated the question :)
– Tiago Porsch