使用Python编写的基本音乐播放器。它可以在电脑上打开音乐文件夹,选择音乐文件进行播放。同时可以实现播放、暂停、停止、下一首、上一首、显示进度条,调节音量,显示播放列表和正在播放歌曲信息等功能。同时它还使用了多线程来实现音乐播放和界面更新分离
时间: 2023-12-29 22:03:46 浏览: 134
python大作业基于pyqt5框架设计的音乐播放器项目源码+超详细注释(带GUI界面).zip
很好的问题!这个基本音乐播放器听起来非常实用。使用Python编写的音乐播放器可以通过调用Python的音乐播放库来实现。Python的音乐播放库有很多种,比如pyglet、pygame等,可以根据需要选择使用。下面是一个简单的示例代码,实现了基本的音乐播放器功能:
```python
import os
import tkinter as tk
import threading
import time
import pygame
class MusicPlayer:
def __init__(self, root):
self.root = root
self.root.title("Music Player")
self.root.geometry("500x400")
self.music_list = []
self.current_music = None
self.paused = False
self.stop = False
self.volume = 0.5
self.music_folder_path = tk.StringVar()
self.music_folder_path.set(os.getcwd())
self.create_widgets()
self.play_thread = threading.Thread(target=self.play_music_thread)
self.play_thread.daemon = True
self.play_thread.start()
self.update_thread = threading.Thread(target=self.update_ui_thread)
self.update_thread.daemon = True
self.update_thread.start()
def create_widgets(self):
tk.Label(self.root, text="Music Folder:").grid(row=0, column=0)
tk.Entry(self.root, textvariable=self.music_folder_path).grid(row=0, column=1)
tk.Button(self.root, text="Open", command=self.open_music_folder).grid(row=0, column=2)
tk.Label(self.root, text="Music List:").grid(row=1, column=0)
self.music_listbox = tk.Listbox(self.root, height=10)
self.music_listbox.grid(row=1, column=1, columnspan=2)
self.music_listbox.bind("<Double-Button-1>", self.play_music)
tk.Button(self.root, text="Play", command=self.play_music).grid(row=2, column=0)
tk.Button(self.root, text="Pause", command=self.pause_music).grid(row=2, column=1)
tk.Button(self.root, text="Stop", command=self.stop_music).grid(row=2, column=2)
tk.Label(self.root, text="Volume:").grid(row=3, column=0)
self.volume_scale = tk.Scale(self.root, from_=0, to=1, resolution=0.01, orient=tk.HORIZONTAL, command=self.set_volume)
self.volume_scale.set(self.volume)
self.volume_scale.grid(row=3, column=1, columnspan=2)
tk.Label(self.root, text="Progress:").grid(row=4, column=0)
self.progress_bar = tk.Scale(self.root, from_=0, to=100, orient=tk.HORIZONTAL)
self.progress_bar.grid(row=4, column=1, columnspan=2)
tk.Label(self.root, text="Now Playing:").grid(row=5, column=0)
self.current_music_label = tk.Label(self.root, text="")
self.current_music_label.grid(row=5, column=1, columnspan=2)
def open_music_folder(self):
self.music_list.clear()
music_folder_path = self.music_folder_path.get()
for filename in os.listdir(music_folder_path):
if filename.endswith(".mp3"):
self.music_list.append(os.path.join(music_folder_path, filename))
self.music_listbox.delete(0, tk.END)
for music in self.music_list:
self.music_listbox.insert(tk.END, os.path.basename(music))
def play_music(self, event=None):
if not self.music_list:
return
if event:
selection = event.widget.curselection()
if not selection:
return
index = selection[0]
else:
index = self.music_list.index(self.current_music) + 1
if index >= len(self.music_list):
index = 0
self.current_music = self.music_list[index]
pygame.mixer.music.load(self.current_music)
pygame.mixer.music.play()
self.paused = False
self.stop = False
self.current_music_label.config(text=os.path.basename(self.current_music))
def pause_music(self):
if pygame.mixer.music.get_busy():
if not self.paused:
pygame.mixer.music.pause()
self.paused = True
else:
pygame.mixer.music.unpause()
self.paused = False
def stop_music(self):
if pygame.mixer.music.get_busy():
pygame.mixer.music.stop()
self.stop = True
def set_volume(self, volume):
self.volume = float(volume)
pygame.mixer.music.set_volume(self.volume)
def play_music_thread(self):
while True:
if not self.stop and not self.paused and pygame.mixer.music.get_busy():
time.sleep(0.1)
elapsed_time = pygame.mixer.music.get_pos() / 1000
total_time = pygame.mixer.Sound(self.current_music).get_length()
if elapsed_time >= total_time:
self.play_music()
progress = int(elapsed_time / total_time * 100)
self.progress_bar.set(progress)
else:
time.sleep(0.1)
def update_ui_thread(self):
while True:
if pygame.mixer.music.get_busy():
self.root.title("Music Player - Playing")
else:
self.root.title("Music Player")
time.sleep(0.1)
if __name__ == "__main__":
pygame.mixer.init()
root = tk.Tk()
app = MusicPlayer(root)
root.mainloop()
```
在这个示例代码中,我们使用了pygame.mixer库来实现音乐播放功能。同时,使用了tkinter库来实现界面。在界面中,我们可以选择音乐文件夹,然后显示音乐列表,在列表中双击可以播放音乐。同时,我们可以通过控制界面上的按钮来实现暂停、停止、下一首、上一首等功能。进度条和音量调节器也可以用来控制播放。同时,我们使用了两个线程,一个线程用来实现音乐播放功能,另一个线程用来实现界面更新和进度条的更新。这样可以保证音乐播放和界面更新分离,不会影响音乐播放的效果。
阅读全文