import pygame
import tkinter as tk
from tkinter import filedialog
# 初始化 pygame - 使用下面正常工作示例中的初始化参数
pygame.init()
pygame.mixer.init(buffer=16384)
current_song_index = 0
def play_music():
selected_indices = song_list.curselection()
if selected_indices:
global current_song_index
current_song_index = selected_indices[0]
file_path = song_list.get(current_song_index)
if file_path.lower().endswith('.wav'):
# WAV文件使用新的播放逻辑
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
elif file_path.lower().endswith(('.mp3', '.ogg')):
# MP3和OGG保持原来的播放逻辑
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
def pause_music():
pygame.mixer.music.pause()
def continue_music():
pygame.mixer.music.unpause()
def stop_music():
pygame.mixer.music.stop()
def next_song():
global current_song_index
if current_song_index < song_list.size() - 1:
current_song_index += 1
else:
current_song_index = 0
file_path = song_list.get(current_song_index)
if file_path.lower().endswith(('.mp3', '.wav', '.ogg')):
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
def previous_song():
global current_song_index
if current_song_index > 0:
current_song_index -= 1
else:
current_song_index = song_list.size() - 1
file_path = song_list.get(current_song_index)
if file_path.lower().endswith(('.mp3', '.wav', '.ogg')):
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
def open_file():
file_paths = filedialog.askopenfilenames(
filetypes=[
("All Files", "*.*"), # 新增全部文件筛选
("MP3 Files", "*.MP3"),
("WAV Files", "*.wav"),
("OGG Files", "*.ogg")
]
)
for file_path in file_paths:
song_list.insert(tk.END, file_path)
root = tk.Tk()
root.title("音乐播放器")
# 设置窗口大小
root.geometry("800x600")
song_list = tk.Listbox(root,background='cyan', selectmode=tk.MULTIPLE)
song_list.place(x=100, y=30, width=400, height=200) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
song_list.config(selectmode=tk.MULTIPLE)
play_button = tk.Button(root,background='green', text="播放")
play_button.place(x=100, y=260, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
play_button.config(command=play_music)
pause_button = tk.Button(root,background='yellow',text="暂停", command=pause_music)
pause_button.place(x=300, y=260, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
pause_button.config(command=pause_music)
continue_button = tk.Button(root,background='blue', text="继续", command=continue_music)
continue_button.place(x=300, y=380, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
continue_button.config(command=continue_music)
stop_button = tk.Button(root,background='red', text="停止", command=stop_music)
stop_button.place(x=100, y=380, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
stop_button.config(command=stop_music)
previous_button = tk.Button(root,background='orange', text="上一首", command=previous_song)
previous_button.place(x=500, y=260, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
previous_button.config(command=previous_song)
next_button = tk.Button(root,background='orange', text="下一首", command=next_song)
next_button.place(x=500, y=380, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
next_button.config(command=next_song)
open_button = tk.Button(root,background='purple', text="打开文件", command=open_file)
open_button.place(x=550, y=90, width=100, height=50) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
open_button.config(command=open_file)
root.mainloop()
手机端Python语言音乐播放器程序代码QZQ-2025-5-31
最新推荐文章于 2025-06-05 17:17:43 发布