-
Notifications
You must be signed in to change notification settings - Fork 0
Adjust the sound volume while playing #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package com.dinosaur.dinosaurexploder.controller; | ||
import javafx.application.Platform; | ||
import javafx.beans.value.ChangeListener; | ||
import javafx.beans.value.ObservableValue; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.Slider; | ||
import javafx.scene.image.Image; | ||
import javafx.scene.image.ImageView; | ||
import javafx.scene.layout.BorderPane; | ||
import javafx.scene.media.Media; | ||
import javafx.scene.media.MediaPlayer; | ||
|
||
public class SoundController { | ||
private static SoundController instance; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Consider adding JavaDoc to the |
||
private MediaPlayer inGameSound; | ||
private BorderPane root; | ||
private static double volume = 1.0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improvement: Consider initializing the |
||
private static double prevVolume = 1.0; | ||
private static double sfxVolume = 1.0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improvement: The |
||
private static double prevSfxVolume = 1.0; | ||
private static Image viewImage; | ||
private static Image viewSfxImage; | ||
private SoundController() {} | ||
|
||
public static SoundController getInstance() { | ||
if (instance == null) { | ||
instance = new SoundController(); | ||
} | ||
return instance; | ||
} | ||
|
||
public void playInGameSound(String musicResource, double volumeValue) { | ||
if(inGameSound != null) | ||
{ | ||
inGameSound.stop(); | ||
} | ||
Media media = new Media(getClass().getResource(musicResource).toExternalForm()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Consider adding a check for null before accessing the resource to prevent potential if (getClass().getResource(musicResource) == null) {
System.err.println("Resource not found: " + musicResource);
return;
} |
||
inGameSound = new MediaPlayer(media); | ||
inGameSound.setCycleCount(MediaPlayer.INDEFINITE); | ||
inGameSound.setVolume(volume); | ||
inGameSound.play(); | ||
} | ||
|
||
public void stopInGameSound() { | ||
if (inGameSound != null) { | ||
inGameSound.stop(); | ||
} | ||
} | ||
|
||
public void muteInGameSound() { | ||
if (inGameSound != null) { | ||
inGameSound.setMute(true); | ||
} | ||
} | ||
|
||
public void unmuteInGameSound() { | ||
if (inGameSound != null) { | ||
inGameSound.setMute(false); | ||
} | ||
} | ||
|
||
public boolean isMuted() { | ||
return inGameSound != null && inGameSound.isMute(); | ||
} | ||
public double getVolume() { | ||
return volume; | ||
} | ||
public double getSfxVolume() { | ||
return sfxVolume; | ||
} | ||
public Image getViewSfxImage() { | ||
return viewSfxImage; | ||
} | ||
public Image getViewImage() { | ||
return viewImage; | ||
} | ||
public void playSoundEffect(String soundResource) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Consider adding proper error handling when playing sound effects. If the sound file is missing, the current implementation might throw an exception. |
||
// Use the controlled volume | ||
Media media = new Media(getClass().getResource(soundResource).toExternalForm()); | ||
MediaPlayer sfxPlayer = new MediaPlayer(media); | ||
sfxPlayer.setVolume(sfxVolume); | ||
sfxPlayer.play(); | ||
} | ||
public double adjustInGameSFX(Slider sfxVolumeSlider, Label sfxVolumeLabel, Image sfxMute, Image sfxAudioOn, ImageView sfxImageViewPlaying) { | ||
sfxVolumeSlider.setValue(sfxVolume); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Consider adding a null check for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Design Improvement: The |
||
sfxVolumeSlider.valueProperty().addListener(new ChangeListener<Number>() { | ||
@Override | ||
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) { | ||
Platform.runLater(() -> { | ||
sfxVolume = newValue.doubleValue(); | ||
sfxVolumeLabel.setText(String.format("%.0f%%", sfxVolume * 100)); | ||
System.out.println("VFX Volume label updated to: " + sfxVolumeLabel.getText()); | ||
if(sfxVolume == 0.00) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Best Practice: Consider using a logger instead of |
||
sfxImageViewPlaying.setImage(sfxMute); | ||
viewSfxImage = sfxMute; | ||
} | ||
else | ||
{ | ||
sfxImageViewPlaying.setImage(sfxAudioOn); | ||
viewSfxImage = sfxAudioOn; | ||
} | ||
}); | ||
} | ||
|
||
}); | ||
|
||
sfxImageViewPlaying.setOnMouseClicked(mouseEvent -> { | ||
if (sfxVolume == 0.00){ | ||
sfxImageViewPlaying.setImage(sfxAudioOn); | ||
sfxVolume = prevSfxVolume; | ||
sfxVolumeSlider.setValue(sfxVolume); | ||
viewSfxImage = sfxAudioOn; | ||
} else { | ||
sfxImageViewPlaying.setImage(sfxMute); | ||
prevSfxVolume = sfxVolume; | ||
sfxVolume = 0.00; | ||
sfxVolumeSlider.setValue(sfxVolume); | ||
viewSfxImage = sfxMute; | ||
} | ||
}); | ||
return sfxVolume; | ||
} | ||
public double adjustVolume(Slider volumeSlider, Label volumeLabel, Image mute, Image audioOn, ImageView imageViewPlaying) { | ||
volumeSlider.setValue(volume); | ||
volumeSlider.valueProperty().addListener(new ChangeListener<Number>() { | ||
@Override | ||
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) { | ||
Platform.runLater(() -> { | ||
|
||
volume = newValue.doubleValue(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improvement: Consider adding a null check for the |
||
inGameSound.setVolume(volume); | ||
volumeLabel.setText(String.format("%.0f%%", volume * 100)); | ||
System.out.println("Volume label updated to: " + volumeLabel.getText()); | ||
if(volume == 0.00) | ||
{ | ||
imageViewPlaying.setImage(mute); | ||
viewImage = mute; | ||
} | ||
else | ||
{ | ||
imageViewPlaying.setImage(audioOn); | ||
viewImage = audioOn; | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
|
||
imageViewPlaying.setOnMouseClicked(mouseEvent -> { | ||
if (volume == 0.00){ | ||
imageViewPlaying.setImage(audioOn); | ||
volume = prevVolume; | ||
volumeSlider.setValue(volume); | ||
viewImage = audioOn; | ||
} else { | ||
imageViewPlaying.setImage(mute); | ||
prevVolume = volume; | ||
volume = 0.00; | ||
volumeSlider.setValue(volume); | ||
viewImage = mute; | ||
} | ||
}); | ||
|
||
return volume; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,10 @@ | |
* This holds every constant in the the PROJECT | ||
*/ | ||
public class GameConstants { | ||
|
||
/* | ||
* CONSTANTS FOR IMAGES | ||
*/ | ||
* CONSTANTS FOR IMAGES | ||
*/ | ||
public static final String BACKGROUND_IMAGEPATH = "/assets/textures/background.png"; | ||
public static final String SPACESHIP_IMAGEPATH = "assets/textures/spaceship.png"; | ||
public static final String SPACESHIP_IMAGEFILE = "spaceship.png"; | ||
|
@@ -23,22 +23,22 @@ public class GameConstants { | |
public static final String GREENDINO_IMAGEFILE = "greenDino.png"; | ||
public static final String HEART_IMAGEPATH = "assets/textures/life.png"; | ||
/* | ||
*CONSTANTS FOR FONTS | ||
*/ | ||
*CONSTANTS FOR FONTS | ||
*/ | ||
public static final String ARCADECLASSIC_FONTNAME = "ArcadeClassic"; | ||
/* | ||
* SOUNDS | ||
*/ | ||
public static final String ENEMYSHOOT_SOUND = "enemyShoot.wav"; | ||
public static final String SHOOT_SOUND = "shoot.wav"; | ||
* SOUNDS | ||
*/ | ||
public static final String ENEMYSHOOT_SOUND = "/assets/sounds/enemyShoot.wav"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consistency: The sound path constants in |
||
public static final String SHOOT_SOUND = "/assets/sounds/shoot.wav"; | ||
public static final String MAINMENU_SOUND = "/assets/sounds/mainMenu.wav"; | ||
public static final String BACKGROUND_SOUND ="gameBackground.wav"; | ||
public static final String ENEMY_EXPLODE_SOUND = "enemyExplode.wav"; | ||
public static final String PLAYER_HIT_SOUND = "playerHit.wav"; | ||
public static final String BACKGROUND_SOUND ="/assets/sounds/gameBackground.wav"; | ||
public static final String ENEMY_EXPLODE_SOUND = "/assets/sounds/enemyExplode.wav"; | ||
public static final String PLAYER_HIT_SOUND = "/assets/sounds/playerHit.wav"; | ||
|
||
public static final String GAME_NAME = "Dinosaur Exploder"; | ||
|
||
public static final List<Language> AVAILABLE_LANGUAGES = List.of(Language.ENGLISH, Language.GERMAN ); | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improvement: The
inGameSound
field is now declared in bothDinosaurController
andSoundController
. SinceSoundController
manages the sound, consider removing the duplicate field fromDinosaurController
.