Python Forum
Returning to previous volume level after Mute
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returning to previous volume level after Mute
#1
I have just written an mp3 player code (using pygame), and so far most of the functions are working. However, I am attempting to figure out how to mute and then unmute with the same volume level that the user set after adjusting the volume using the Scale bar.

The mute button is working but when I unmute on the second click of the button the volume that the use has set using the Scale widget it will not return to that volume.
I have tried using the get_volume() feature and this will give me the current volume prior to the function setting it to "0" (tested it using a print statement), but when I try to use the value stored in "prev_volume" to return it to the volume the were it was set before clicking the mute button it does not use the value in the variable and the volume remains at "0".

def mute():
    prev_volume= pygame.mixer.music.get_volume()
    if mute_button['text'] == 'mute':
        pygame.mixer.music.set_volume(0)
        mute_button['text'] = 'unmute'

    elif mute_button['text'] == 'unmute':
        pygame.mixer.music.set_volume(prev_volume)
        mute_button['text'] = 'mute'
    print(prev_volume)
This will mute the sound (volume is set to "0").
But when the mute button is pressed again it should return to the value of "prev_volume" and not stay at "0".

Any suggestions on how I can get the value of the volume prior to the mute to be reinstated when the button is pressed the second time?
"Often stumped... But never defeated."
Reply


Messages In This Thread
Returning to previous volume level after Mute - by DT2000 - Jul-23-2019, 04:22 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020