I'm working on a code that is supposed to be a generator of random noises and has 3 main functions - playing noises infinitely, stop and exit. The problem begins when it comes to play next sound. I tried to use pygame.mixer.music.queue()
, but it didn't seem to work at all. With the use of pygame.mixer.music.set_endevent()
after hitting 'PLAY' I wasn't able to use any another functionality. Now my code looks like below, it plays the next noise correctly but the problem is that hitting 'STOP' doesn't stop it from playing but starts playing another noise. Can you please help me fixing this?
import os
import pygame
from tkinter import *
import random
import sys
playlista = []
pygame.init()
def start_noise(event):
directory = "C:/Users/directory/with/noises"
os.chdir(directory)
for files in os.listdir(directory):
if files.endswith(".ogg"):
playlista.append(files)
pygame.mixer.init()
next_noise = random.choice(playlista)
pygame.mixer.music.load(next_noise)
pygame.mixer.music.play()
different_noise()
def different_noise():
pos = pygame.mixer.music.get_pos()
if int(pos) == -1:
next_noise2 = random.choice(playlista)
pygame.mixer.music.load(next_noise2)
pygame.mixer.music.play()
root.after(1, different_noise)
def stop_noise(event):
pygame.mixer.music.stop()
def exit_noise(event):
sys.exit()
root = Tk()
playbutton = Button(root,text = "PLAY")
playbutton.pack()
stopbutton = Button(root,text = "STOP")
stopbutton.pack()
exitbutton = Button(root, text="EXIT")
exitbutton.pack()
playbutton.bind("<Button-1>",start_noise)
stopbutton.bind("<Button-1>",stop_noise)
exitbutton.bind("<Button-1>",exit_noise)
root.minsize(200,80)
root.mainloop()