1
package com.first.serious.main.controls;

import java.io.BufferedInputStream;

import java.io.InputStream;

import javax.sound.sampled.*;


public class AudioPlayer {

private Clip clip;


public AudioPlayer(String path){
    try{

        AudioInputStream AIS = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(path));
        InputStream bufferedIn = new BufferedInputStream(AIS);
        AudioInputStream audiostream = AudioSystem.getAudioInputStream(bufferedIn);

        /* 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(audiostream);


    }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();
}
}

Honestly, I just copied the code because I am a complete beginner in game developing. Already had two sets of code, the commented lines are from the first.

I used it in this class for mouseinputs

package com.first.serious.main.controls;

import com.first.serious.main.Shooter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MouseInput implements MouseListener{

   private Shooter game;
   private AudioPlayer deadsound;


   public MouseInput(Shooter game){
       this.game = game;
      deadsound = new AudioPlayer("/res/sounds/deadsound2.mp3");
   }
@Override
public void mouseClicked(MouseEvent me) {

}

@Override
public void mousePressed(MouseEvent me) {
    int mx = me.getX();
    int my = me.getY();
    //System.out.println(mx + " "  + my);
    if(game.state.equals("menu")){
        if(mx > 60 && mx < 330 && my > 200 && my < 280){
            deadsound.play();
            game.state = "game";
        }else
         if(mx > 60 && mx < 330 && my > 460 && my < 540){
            System.exit(0);
        }else
          if(mx > 60 && mx < 330 && my > 330 && my < 410){
              game.state = "help";
          }
    }
    if(game.state.equals("Dead")){
       // System.out.println("dead");
        if(mx > 60 && mx < 330 && my > 260 && my < 340){
            game.Score = 0;
            game.state = "game";
        }else
        if(mx > 60 && mx < 330 && my > 390 && my < 470){
            System.exit(0);
        }
    }

    if(game.state.equals("Upgrade")){
        if(mx > 60 && mx < 330 && my > 200 && my < 280){
            game.upgradectr = 0;
            game.player.XX += 0.5f;
            game.state2++;
            game.state = "game";
        }else
        if(mx > 60 && mx < 330 && my > 330 && my < 410){
            game.upgradectr = 0;
            game.getControl().Vy -= 1f;
            game.state2++;
            game.state = "game";
        }else
        if(mx > 60 && mx < 330 && my > 460 && my < 540){
             if(game.state2==05||game.state2==10||game.state2==20){
                game.upgradectr = 0;
                game.player.a += 2;
                game.state2++;
                game.state = "game";
            }else{   
                game.upgradectr = 0;
                System.out.println(game.upgradectr);
                game.getControl().reload -= 2;
                game.state2++;
                System.out.println(game.state2);
                game.state = "game";
             }
        }

    }else
    if(game.state.equals("help")){
        if(mx > 60 && mx < 330 && my > 460 && my < 540){
            game.state = "menu";
        }
    }
}

@Override
public void mouseReleased(MouseEvent me) {
}

@Override
public void mouseEntered(MouseEvent me) {
}

@Override
public void mouseExited(MouseEvent me) {
}

}

and then get these errors

java.lang.NullPointerException
at javazoom.spi.mpeg.sampled.file.MpegAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1113)
at com.first.serious.main.controls.AudioPlayer.<init>(AudioPlayer.java:17)
at com.first.serious.main.controls.MouseInput.<init>(MouseInput.java:15)
at com.first.serious.main.Shooter.init(Shooter.java:75)
at com.first.serious.main.Shooter.run(Shooter.java:670)
at java.lang.Thread.run(Thread.java:748)

I don't absolutely know what is the exact problem. I have the following jar libraries: mp3spi1.9.5.jar, jl1.0.1.jar and, tritonus_share.jar. Please help. Thank you.

directories

Please see the directories link for reference. thanks!. Realized the problem now, but haven't fully understood. I'm sorry guys, but my problem now is, how can I access the files from the sound folder while my java file is in the controls folder. I tried putting the file inside where the java file is and it worked. But I want my files to be organized as much as I could. Please Help thank you.

  • `getResourceAsStream(path)` returns `null` if it can't find the path. See the [DOCS](https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)). – QBrute Nov 13 '17 at 09:58
  • if you get that kind of error and don't know what it means, stop fiddling around with third party libraries, put creating games on a hold and first learn the basics. – Stultuske Nov 13 '17 at 09:59
  • I'm pretty sure I have encoded the correct path of the file, if there would be a mistake, ot might be just the ' / ' but I have already tried both but still get the same results. – Villa Zenpai Nov 13 '17 at 10:19
  • At first, I wantes to finish the game without any kind of imports such as images and sounds, but obviously, irony hits now. If I would not get past this, I might not include sounds anymore. – Villa Zenpai Nov 13 '17 at 10:21

0 Answers0