Skip to content
Advertisement

Java Stop Sound Button Not Working Correctly

So I have been making a stop button recently and have been wondering how can I stop a button sound instantly when already playing.

The problem : When you click the stop sound button, it only stops the next button you press.

What Im Trying to Achieve : When you click the stop sound button, it stops all playing sounds.

Here is the main button class sound :

JavaScript

Here is the main sound stop button :

JavaScript

Advertisement

Answer

The problem is due to the logic with the .stop field in one JButton modified by the other JButton. Also reusing playSound() to play or stop is not a good design.

Here is a solution with a much cleaner design with 2 buttons, one for play one for stop.

The MusicController is independent from the UI:

JavaScript

The View manages the UI and just listens to the MusicController state.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement