Python 实现自定义时间音乐播放器
在 Python 中可以使用tkinter
来创建播放器的按钮,通过keyboard
获取键盘的输入,通过pygame
来操作音乐的播放,话不多说直接上代码
。
import os
import random
import time
import keyboard
import pygame
import tkinter as tk
class Player:
def __init__(self, music_path):
# 设置音乐文件夹路径
self.music_path = music_path
# 获取音乐文件列表
self.music_list = [file for file in os.listdir(self.music_path) if file.endswith('.mp3')]
self.current_music_file = None
self.is_running = True
# 初始化pygame
pygame.init()
# 创建主窗口
self.root = tk.Tk()
self.root.title("MP3 Player")
self.root.geometry("250x50")
# 创建控制界面
self.control_panel = tk.Frame(self.root)
self.control_panel.pack()
self.play_button = tk.Button(self.control_panel, text="播放\nSpace 键", command=self._play_random_music)
self.play_button.pack(side=tk.LEFT, padx=5)
self.next_button = tk.Button(self.control_panel, text="下一曲\nN 键", command=self._play_next_music)
self.next_button.pack(side=tk.LEFT, padx=5)
self.quit_button = tk.Button(self.control_panel, text="退出\nQ 键", command=self._quit)
self.quit_button.pack(side=tk.RIGHT, padx=5)
def _quit(self):
self.root.destroy
pygame.mixer.music.stop()
print("退出播放器")
self.is_running = False
def _select_random_music_file(self):
# 随机选择一个音乐文件
select_file = random.choice(self.music_list)
if not self.current_music_file:
select_file = random.choice(self.music_list)
else:
while self.current_music_file == select_file:
select_file = random.choice(self.music_list)
self.current_music_file = select_file
return select_file
def _play_random_music(self):
# 已经启动音乐了
if self.is_playing:
# 正在播放,让它暂停
if pygame.mixer.music.get_busy():
pygame.mixer.music.pause()
self.play_button.config(text="播放\nSpace 键")
self.play_button.pack()
else:
# 暂停状态,让它播放
pygame.mixer.music.unpause()
self.play_button.config(text="暂停\nSpace 键")
self.play_button.pack()
else:
self.is_playing = True
self._select_random_music_file()
music_file = os.path.join(self.music_path, self.current_music_file)
# 加载音乐文件
pygame.mixer.music.load(music_file)
# 播放音乐
pygame.mixer.music.play()
self.play_button.config(text="暂停\nSpace 键")
self.play_button.pack()
def _play_next_music(self):
pygame.mixer.music.stop()
self.is_playing = False
self._play_random_music()
def run(self):
next_play_time = time.time() + random.randint(100, 500)
self.is_playing = False
# 主循环
while self.is_running:
self.root.update_idletasks()
self.root.update()
# 自动播放音乐
if time.time() > next_play_time:
self.is_playing = True
self._play_random_music()
next_play_time = time.time() + random.randint(100, 500)
# 快捷键 n 键播放下一曲
elif keyboard.is_pressed('n'):
self.is_playing = True
self._play_next_music()
# 空格键控制播放/暂停
elif keyboard.is_pressed(' '):
if pygame.mixer.music.get_busy():
pygame.mixer.music.pause()
self.play_button.config(text="播放\nSpace 键")
self.play_button.pack()
else:
if not self.is_playing:
self.is_playing = True
self._play_random_music()
else:
pygame.mixer.music.unpause()
self.play_button.config(text="暂停\nSpace 键")
self.play_button.pack()
# q键退出
elif keyboard.is_pressed('q'):
print("退出播放器")
pygame.mixer.music.stop()
break
# 防止连续触发
time.sleep(0.2)
if __name__ == "__main__":
player = Player("music")
player.run()
这样就能直接生成自定义的音乐播放器,注意,你的音乐应该放在music
文件夹中。