手机端Python语言音乐播放器程序代码QZQ-2025-5-31

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()





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EYYLTV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值