Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

arvi18
Copy link

@arvi18 arvi18 commented Apr 15, 2025

👉 Checklist

Please make sure to check off the following before submitting:

  • I have reviewed my submission thoroughly.
  • I have tested my code (if submission is related to coding) and run the game before pushing (to make sure the project compile).
  • I have run the JUnit tests (if submission is related to coding).
  • I have read the Code of Conduct and Contribution Guidelines.

✍ Description of the Pull Request

Please concisely describe the changes you have made.

  • Added Volume Control for Background Music and Added Sound Effects Volume Control

🔗 Issue link

Issue reference number and link goes here e.g. 'Fixes/Closes #<issue_number>'.

  • This Pull Request fixes the issue : 'Closes #'
  • This Pull Request does not fix an issue.

Thanks for taking the time to fill out this Pull Request! ❤️ Thanks for contributing to this project 🦖

@arvi18
Copy link
Author

arvi18 commented Apr 28, 2025

/codehelper hello

private static SoundController instance;
private MediaPlayer inGameSound;
private BorderPane root;
private static double volume = 1.0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improvement: Consider initializing the inGameSound field in the constructor rather than having it as null initially. This would prevent potential null pointer exceptions if methods like stopInGameSound() are called before playInGameSound().

{
inGameSound.stop();
}
Media media = new Media(getClass().getResource(musicResource).toExternalForm());

Choose a reason for hiding this comment

The 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 NullPointerException if the resource is not found:

if (getClass().getResource(musicResource) == null) {
    System.err.println("Resource not found: " + musicResource);
    return;
}

sfxVolumeLabel.setText(String.format("%.0f%%", sfxVolume * 100));
System.out.println("VFX Volume label updated to: " + sfxVolumeLabel.getText());
if(sfxVolume == 0.00)
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best Practice: Consider using a logger instead of System.out.println() for better control over logging output.

private BorderPane root;
private static double volume = 1.0;
private static double prevVolume = 1.0;
private static double sfxVolume = 1.0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improvement: The prevVolume and prevSfxVolume fields are static but could be instance variables since they're specific to each instance of SoundController. Since this is a singleton, it might not cause issues, but it's better practice to make them instance variables.

sfxPlayer.play();
}
public double adjustInGameSFX(Slider sfxVolumeSlider, Label sfxVolumeLabel, Image sfxMute, Image sfxAudioOn, ImageView sfxImageViewPlaying) {
sfxVolumeSlider.setValue(sfxVolume);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Consider adding a null check for the sfxImageViewPlaying parameter before accessing it to prevent potential NullPointerException.

sfxPlayer.play();
}
public double adjustInGameSFX(Slider sfxVolumeSlider, Label sfxVolumeLabel, Image sfxMute, Image sfxAudioOn, ImageView sfxImageViewPlaying) {
sfxVolumeSlider.setValue(sfxVolume);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design Improvement: The adjustInGameSFX and adjustVolume methods are quite long and do multiple things. Consider breaking them down into smaller, more focused methods for better maintainability.

public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
Platform.runLater(() -> {

volume = newValue.doubleValue();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improvement: Consider adding a null check for the inGameSound before setting its volume to prevent potential NullPointerException.

import javafx.scene.media.MediaPlayer;

public class SoundController {
private static SoundController instance;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Consider adding JavaDoc to the SoundController class and its public methods to better document their purpose and usage.

@@ -20,7 +22,7 @@ public class DinosaurController {
private Entity score;
private Entity life;
private int lives = 3;

private MediaPlayer inGameSound;

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 both DinosaurController and SoundController. Since SoundController manages the sound, consider removing the duplicate field from DinosaurController.

public Image getViewImage() {
return viewImage;
}
public void playSoundEffect(String soundResource) {

Choose a reason for hiding this comment

The 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.

public PauseMenu() {
super(MenuType.GAME_MENU);
DinosaurApp.initLanguages();

DinosaurApp.initLanguages();

PauseButton btnBack = new PauseButton(getLocalizationService().getLocalizedString("Pause.1"),() -> fireResume());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Style: The comment //need2ComeBack seems like a TODO item. Consider using a proper TODO comment format like // TODO: [description] or removing it if no longer needed.

//These are for tracking how many times the pauseMenu has been opened. Helps in getting the mute/unmute image from SoundController
//For first time, this helps loading the unmuted icon and if count increases more than 1, it changes the icon depending on the controller
//At this point of time, I really don't have other proper method, It works so why not, But if you know any other solution please let me know and contribute
private int count = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design Concern: The comment about tracking how many times the pause menu has been opened suggests a workaround solution. Consider refactoring this approach to a more robust design that doesn't rely on counting menu opens.


ControlButton btnControls = new ControlButton(getLocalizationService().getLocalizedString("Pause.3"));


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resource Management: Consider using try-with-resources for handling input streams to ensure they are properly closed after use.


//These are for tracking how many times the pauseMenu has been opened. Helps in getting the mute/unmute image from SoundController
//For first time, this helps loading the unmuted icon and if count increases more than 1, it changes the icon depending on the controller
//At this point of time, I really don't have other proper method, It works so why not, But if you know any other solution please let me know and contribute

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improvement: The count and sfxCount variables are used to track menu opens, but this approach seems fragile. Consider using a more direct approach to check the current state from SoundController instead.

public static final String SHOOT_SOUND = "shoot.wav";
* SOUNDS
*/
public static final String ENEMYSHOOT_SOUND = "/assets/sounds/enemyShoot.wav";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency: The sound path constants in GameConstants have been updated to include the /assets prefix, which is good for consistency, but ensure all code that uses these constants has been updated accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant