Error in AudioInputStream ais = AudioSystem.getAudioInputStream (getClass (). getResourceAsStream (s));

1
  

I create a class to create 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();
}
}
  

So I try to put it in 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 this error:

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 anyone help me ???

EDIT:

I had JDK 7, I installed 8, because of JavaFX, but I have a problem with 8 ... that's another story ...

But, I got this AudioPlayer of this video:

  

link

And he used a song in MP3, so I'm kind of dubious ....

    
asked by anonymous 28.07.2014 / 20:28

2 answers

1

You can load the stream with no problem here:?

bgMusic = new AudioPlayer("Resources/Musics/menu.mp3");

The path to the file seems to me wrong. In the end it will throw a null pointer exception because it can not load the streams. First try using a full path, eg:

c:/arquivo.mp3

Then check the path relative to the file .mp3

Edit:

You can not really play what you're looking for, as far as I'm looking for, AudioPlayer only plays .wav or .mid , but I found it a great alternative to your case here :

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
...
String bip = "bip.mp3";
Media hit = new Media(bip);
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();

Note that they are from the javafx packages.

    
28.07.2014 / 22:13
1

If you want to run an MP3 file in JAVA you can import the BigClip for your project and adding mp3plugin.jar to your build path.

This is just to implement the following piece of code to your program:

BigClip musica = new BigClip();

musica.open(AudioSystem.getAudioInputStream(new File("arquivo.mp3")));

musica.start();

Do not forget to add a JFrame or an infinite loop to keep your program from running.

    
01.10.2015 / 07:35