import pygame from tkinter import * class MediaPlayer: def __init__(self, master): self.master = master master.title("Media Player") # 播放控制按钮 self.play_button = Button(master, text="Play", command=self.play_music) self.pause_button = Button(master, text="Pause", command=self.pause_music) self.stop_button = Button(master, text="Stop", command=self.stop_music) self.loop_button = Checkbutton(master, text="Loop", command=self.toggle_loop) self.play_button.pack() self.pause_button.pack() self.stop_button.pack() self.loop_button.pack() # 创建播放器对象和音乐文件 pygame.mixer.init() #pygame.mixer.music.load("music.m4a") self.loop = False def play_music(self): pygame.mixer.music.play(-1 if self.loop else 0) def pause_music(self): pygame.mixer.music.pause() def stop_music(self): pygame.mixer.music.stop() def toggle_loop(self): self.loop = not self.loop if __name__ == '__main__': root = Tk() mp = MediaPlayer(root) root.mainloop()
该媒体播放器支持m4a格式的音频文件,并添加了循环播放功能。可以通过勾选“Loop”复选框实现。请注意,在Windows上播放m4a格式的音频文件需要安装额外的解码器,如ffmpeg。