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
#2
You need pre_volume to be reachable. After function close it delete. When you enter function you assign to current value. Make it a global or store it as a class variable.

You can just use pause an unpause to get the same effect. No storing variable is needed.
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Windspar, using pause only stops and restarts the file at the same place or time stamp of the mp3 whereas mute will allow the mp3 to continue to execute and play on without the volume and return to the mp3 at what ever place the mp3 is in at the time of unmuting the file.

I have set the variable global and it will now return to the volume set by the user when it is unmuted. The problem I am now finding is that when it returns to the prev_volume set by the user the volume will not allow it to be set higher then is held in the variable and also and then the volume is again set to another level after unmuting and the mute is pressed again and released it returned to the volume from the first user setting and not the new second setting.

I believe the variable prev_volume needs to be cleared after the first mute and unmute and then it should reassign the new assigned value once the user changes the volume again. The variable should then use the new value assigned if the mute button is pressed again?
"Often stumped... But never defeated."
Reply
#4
def mute():
    global prev_volume
    if mute_button['text'] == 'mute':
        # Get volume before setting it to zero
        prev_volume= pygame.mixer.music.get_volume()
        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)
99 percent of computer problems exists between chair and keyboard.
Reply
#5
I had tried this before but I had the line get_volume placed at the wrong line within the function so I removed it. I appreciate you pointing that out. It works as it should now.
"Often stumped... But never defeated."
Reply


Forum Jump:

User Panel Messages

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