function pause_toggle() original_volume_gain = original_volume_gain or mp.get_property_native("volume-gain") local paused = mp.get_property_native("pause") if timer then mp.commandv("print-text", "kill fade-in or fade-out is in progress") niler:kill() if timer then -- in case it was just nil-ed timer:kill() timer = nil end niler = nil end local step = 10 local timeout = 0.05 local total_period = step * timeout local multiplier = 2.0 -- value between 0.25 and 2.0 local gain = original_volume_gain if not paused then local to_step = 1 timer = mp.add_periodic_timer(timeout, function() mp.set_property_native("volume-gain", gain - to_step * multiplier) if to_step == step then mp.set_property_native("pause", true) -- restore original gain while paused mp.set_property_native("volume-gain", gain) paused = true timer:kill() return end to_step = to_step + 1 end) else timer = mp.add_periodic_timer(timeout, function() mp.set_property_native("volume-gain", gain - step * multiplier) if paused then mp.set_property_native("pause", false) paused = false end if step <= 0 then timer:kill() return end step = step - 1 end) end niler = mp.add_timeout(total_period + 2 * timeout, function() timer = nil original_volume_gain = nil end) end mp.add_forced_key_binding("SPACE", "key_space", pause_toggle)