Недавно я сделал Mp3-плеер в Tkinter, используя pygame. Каждая функция работала хорошо, тогда я просто хотел добавить плейлист, чтобы я мог слушать автоматически проигрываемые песни, вместо того, чтобы нажимать «кнопку воспроизведения» для воспроизведения другой песни. Но я не мог справиться с этим.
Я пытался использовать, pygame.mixer.music.get_busy, но либо я не мог использовать его должным образом, либо он не работает. Я не знаю.
from tkinter import *
import pygame, os, random
pygame.mixer.init()
songs = []
for file in os.listdir():
if file.endswith(".mp3"):
songs.append(file)
print(songs)
window=Tk()
window.title("Music Player")
window.geometry("200x120+50+50")
window.resizable(width=False,height=False)
class Player:
def __init__(self,n,selected_song):
self.n = 0
self.x = 2
self.selected_song = random.choice(songs)
self.next = random.choice(songs)
self.volume = 1
pygame.mixer.music.load(self.selected_song)
pygame.mixer.music.play()
def again():
self.selected_song = random.choice(songs)
pygame.mixer.music.load(self.selected_song)
pygame.mixer.music.play()
again()
def play_pause(self):
if self.n==0:
pygame.mixer.music.load(self.selected_song)
pygame.mixer.music.play()
self.n+=2
print("playing")
if pygame.mixer.get_busy() == True:
self.selected_song = random.choice(songs)
pygame.mixer.music.load(self.selected_song)
pygame.mixer.music.play()
elif self.n>0:
if self.n % 2 ==0:
pygame.mixer.music.pause()
print("paused")
self.n+=1
else:
pygame.mixer.music.unpause()
print("unpaused/resuming")
self.n+=1
def stop(self):
pygame.mixer.music.stop()
print("stopped")
self.n = 0
def change(self):
self.selected_song = random.choice(songs)
pygame.mixer.music.load(self.selected_song)
pygame.mixer.music.play()
def volume_down(self):
self.volume -= 0.1
if self.volume == 0:
self.volume = 0
print(self.volume)
def volume_up(self):
self.volume += 0.1
if self.volume == 1.0:
self.volume = 1.0
print(self.volume)
def mute_unmute(self):
if self.x%2==0:
print("muted")
self.x+=1
else:
self.x+=1
print("unmuted")