2

Professor provided us with some video game code and we had to alter/add to it. Been trying to add audio for background music, player colliding with enemy, player dying, colliding with treasure, and firing a gun. I have all the sound files but its been tough trying to integrate them. Im not so sure where to put them or if my code is correct, but this is what I have so far.

I can't get any sound effects or background music to play when I run the game.

     private MediaPlayer backgroundMusicPlayer;
     private String playerCollideEnemyMusicFile =     "player_collide_enemy.wav";
     private String playerDyingMusicFile = "player_die.wav";
     private String playerCollideTreasureMusicFile = "treasure.wav";
     private String playerFiringMusicFile = "player_firing.wav";


 private void playSound(String musicFile) {
    String musicFileUrl = this.getClass().getResource("/resources/" + musicFile).toExternalForm();
    AudioClip audioClip = new AudioClip(musicFileUrl);
    audioClip.play();  
    }

private void playEnemySound() {
    playSound(playerCollideEnemyMusicFile);
    }  

private void playDyingSound() {
    playSound(playerDyingMusicFile);
    }

private void playTreasureSound() {
    playSound(playerCollideTreasureMusicFile);
    }



 } else if (r.getFill().equals(Color.GREEN)) {   // GREEN =    treasure/chest
                Treasure t = getTreasure(r);
                if (!t.isOpen()) {
                    score.set(score.get() + 10);    // increase the  score
                    chestCount--;   // decrease our global chest count to track end of level
                    t.openNow();    // open the chest so it can't be opened twice
                }
                playTreasureSound();

When I run my code, the game runs, but as soon as I do something that's supposed to have a sound effect(collide with enemy, pick up treasure), the game freezes and I get these error messages spamming down my console:

at main.Game.playSound(Game.java:102)
at main.Game.playTreasureSound(Game.java:115)
at main.Game.collisionCheck(Game.java:218)
at main.Game.access$1(Game.java:200)
at main.Game$1.handle(Game.java:240)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.lambda$handle$484(AnimationTimer.java:57)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.handle(AnimationTimer.java:56)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:357)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Mikkii
  • 21
  • 1
  • 2
    What is the exception type / error message? – n247s Jun 05 '19 at 21:08
  • 2
    Do not use applet classes. Use [Media](https://openjfx.io/javadoc/12/javafx.media/javafx/scene/media/Media.html) and [MediaPlayer](https://openjfx.io/javadoc/12/javafx.media/javafx/scene/media/MediaPlayer.html) to play your audio. – VGR Jun 05 '19 at 21:58
  • 1
    Which line is 102? `String musicFileUrl = ...`? In that case the exception if probably a `NullPointerException` caused by the use of the wrong resource path. Check, how your app deploys the resources. Is there a `resources` directory next to the `main` directory in the classpath or are the media files added to the classpath root (or entry equivalent in a `.jar` file)? – fabian Jun 05 '19 at 22:42
  • Line 102 is AudioClip audioClip = new AudioClip(musicFileUrl); – Mikkii Jun 06 '19 at 20:01
  • Questions about addressing resources for audio come up a lot. I spent a little bit of time looking for a good answer to reference. Best I could find on short notice follows. https://stackoverflow.com/questions/32215693/classloader-resource-paths-are-always-absolute Pay notice to relative vs absolute form (depends if first char is "/"). Even though this question pertains to loading jpg, the addressing scheme via classloader.getResource is the same. We would need to see file folder structure to be able to know for sure if your addressing syntax is not reaching the desired field. – Phil Freihofner Jun 07 '19 at 01:33

0 Answers0