I tried something new to fit the code. Now when I press ctrl + s the audio really stops but when my personal assistant plays music again in the same run, the music does not stop as for the first time:
import glob
import os
import keyboard
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
songs = glob.glob("C:\\Users\zivsi\Music\\*.mp3")
import random
song = random.choice(songs)
with open("song_choice.txt", "r") as file:
read = file.read()
if (read == song):
print("switch song")
song = random.choice(songs)
song_name = song.replace("C:\\Users\zivsi\Music\\", "").replace(".mp3", "")
print("song: ", song_name)
pygame.mixer.init()
music = pygame.mixer.music.load(song)
pygame.mixer.music.play()
while (pygame.mixer_music.get_busy()):
pygame.time.Clock().tick(10)
keyboard.add_hotkey("ctrl + s", lambda: pygame.mixer_music.stop())
# if i will press ctrl + s, the music will stop but it's working only once`