"""导入所需要的模块"""
import pygame,sys,re,time
from pygame.locals import *
import os
"""初始化数据"""
pygame.init()
pygame.font.init()
font = pygame.font.SysFont('SimHei', 20)
#设置屏幕
screen = pygame.display.set_mode((484, 710))
screen.fill((0, 0, 0))
pygame.display.set_caption("简陋的播放器")
"""创建歌词解析类"""
class LyricParse:
def __init__(self,path):
self.path = path #歌词文件路径
self.lyrics = [] #歌词列表,保存歌词对象
#加载文件
def load(self):
if os.path.exists(self.path):
with open(self.path,encoding = 'utf-8') as fp:
data = fp.readlines()
#去掉每一句后的换行符,和去掉换行符后的空行
return [value.strip() for value in data if value.strip()]
return False #如果返回False文件不存在
def parse(self):
data = self.load()
lyrics = []
if data:#数据存在
for i in range(len(data)-1,-1,-1):
tmp = data[i].split(']')
tmp = [value for value in tmp if value]
if len(tmp) < 2:
data.pop(i)
else:
data[i] = data[i].replace("[",'')
#解析
for line in data:
line = line.split(']')
for item in line[:-1]:
#把时间字符串分割成列表,并把列表元素转换为浮点数
second = [float(value) for value in re.split(r':|\.',item)]
second = second[0]*60 + second[1] + second[2]/100
# obj = Lyric(second,line[-1]) #生成歌词对象,line[-1]是歌词
lyrics.append([second,line[-1]]) #添加到歌词列表
#排序
lyrics.sort()
return lyrics
def select_songs():
#从指定路径筛选音乐文件
def collect_songs(fidir):
#建立备选列表
musics = []
# 遍历文件路径
for root, dirs, files in os.walk(fidir):
for file in files:
tmp = []
if file.endswith('mp3') or file.endswith('wav'):
# if file.endswith('mp3'):
file = os.path.join(root, file)