Audioinputstream error ais = Audiosystem.getAudioInputStream(getClass().getResourceAsStream(s));

Asked

Viewed 261 times

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:

https://www.youtube.com/watch?v=ar0hTsb9sxM

And he used an MP3 song, so I’m kind of doubtful....

2 answers

1

You can load the stream without problem here: ?

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

The path to the file seems wrong to me. In the end it will release a null Pointer Exception for not being able to load the streams. First try to use a full path, e.g.:

c:/arquivo.mp3

Then check the path relative to the file .mp3

Edit:

It is really not possible to play what you are looking for, as far as I searched, Audioplayer only reprocesses .wav or .mid, but I found a great alternative in 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 javafx packages.

  • 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

  • Updated, from a look.

  • I updated the question :)

1

If you want to run an MP3 file in JAVA you can by importing the class Bigclip for your project and adding the mp3plugin. to your build path.

Once this is done just implement the following piece of code to your program:

BigClip musica = new BigClip();

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

musica.start();

Not forgetting to add a Jframe or an infinite loop to not stop running your program.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.