今日更新内容:
音量条,歌曲封面,窗口标题
main.py:
import sys
import time
import pygame
from pygame.locals import *
import tool
pygame.init()
pygame.mixer.init()
playstatus = 'stop'
mouseststue = "up"
def quit():
try:
pygame.quit()
except pygame.error:
pass
sys.exit()
class Player:
def __init__(self):
pass
@staticmethod
def load(path):
pygame.init()
pygame.mixer.init()
try:
pygame.mixer.music.load(path)
except pygame.error:
tool.mp4_mp3(path, tool.ExtensionNameChangeover(path, 'mp3'))
pygame.mixer.music.load(tool.ExtensionNameChangeover(path, 'mp3'))
@staticmethod
def play():
global playstatus
try:
pygame.mixer.music.play()
playstatus = 'playing'
except pygame.error:
pygame.mixer.init()
pygame.mixer.music.load(r"example.mp3")
pygame.mixer.music.play()
@staticmethod
def stop():
try:
pygame.mixer.music.pause()
except pygame.error:
pass
@staticmethod
def unstop():
try:
pygame.mixer.music.unpause()
except pygame.error:
pass
@staticmethod
def set_volume(voice):
try:
pygame.mixer.music.set_volume(voice)
except pygame.error:
pass
@staticmethod
def get_volume():
try:
return pygame.mixer.music.get_volume()
except pygame.error:
return
@staticmethod
def getpos():
try:
return pygame.mixer.music.get_pos()
except pygame.error:
return
@staticmethod
def setpos(pos):
try:
pygame.mixer.music.set_pos(pos)
except pygame.error:
pass
pygame.display.set_caption("musicplayer")
screen = pygame.display.set_mode((1000, 800), 0, 32)
filepath = "example.mp3"
def blit_alpha(target, source, location, opacity):
x = location[0]
y = location[1]
temp = pygame.Surface((source.get_width(), source.get_height())).convert()
temp.blit(target, (-x, -y))
temp.blit(source, (0, 0))
temp.set_alpha(opacity)
target.blit(temp, location)
def main():
loudup_x = 400
setloud = False
global filepath, playstatus, screen, mouseststue
play = Player
statusbar = pygame.image.load(r'./lib/statusbar.bmp')
ps = pygame.image.load(r'./lib/ps.bmp').convert_alpha()
musicimg = pygame.image.load('./lib/playimg.bmp')
startmenu = pygame.image.load('./lib/startmenu.bmp')
louddown = pygame.image.load('./lib/louddown.bmp')
loudup = pygame.image.load('./lib/loudup.bmp')
tip1 = pygame.font.Font(r"C:\Users\GBX08\PycharmProjects\musicplayer\lib\FZPTYJW.TTF", 40)
tipa = tip1.render('将文件拖拽至窗口,就可以播放音乐了', True, (0, 0, 255))
while True:
musicloud = loudup_x - 100
# stylist = pygame.key.get_pressed()
mouse_x, mouse_y = pygame.mouse.get_pos()
screen.fill((100, 200, 100))
time.sleep(0.01)
for event in pygame.event.get():
if event.type == QUIT:
quit()
if event.type == pygame.DROPFILE:
filepath = event.file
pygame.display.set_caption(tool.returnfliename(filepath))
play.load(filepath)
try:
musicimg = pygame.image.load(tool.getmusicimg(filepath))
except TypeError:
musicimg = pygame.image.load('./lib/playimg.bmp')
play.play()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if playstatus == 'stop':
play.unstop()
playstatus = 'playing'
else:
play.stop()
playstatus = 'stop'
if event.type == pygame.MOUSEBUTTONDOWN:
mouseststue = 'down'
if mouse_x < 200 and mouse_y < 100:
play.load(filepath)
play.play()
if event.type == pygame.MOUSEBUTTONUP:
setloud = False
mouseststue = 'up'
if loudup_x < mouse_x < loudup_x + 10 and 720 < mouse_y < 730 and mouseststue == 'down':
setloud = True
if setloud:
if 100 < mouse_x < 900:
loudup_x = mouse_x
screen.blit(statusbar, [0, 0])
screen.blit(tipa, [250, 100])
screen.blit(musicimg, [300, 200])
screen.blit(startmenu, [0, 700])
screen.blit(louddown, [100, 720])
screen.blit(loudup, [loudup_x, 720])
if mouse_x < 200 and mouse_y < 700:
blit_alpha(screen, ps, [0, mouse_y - mouse_y % 100], 100)
pygame.display.set_caption(tool.returnfliename(filepath))
pygame.display.flip()
play.set_volume(musicloud / 800)
if __name__ == '__main__':
main()
tool.py:
import os
import mutagen
def mp4_mp3(mp4path, mp3path):
from moviepy import editor
video = editor.VideoFileClip(mp4path)
audio = video.audio
audio.write_audiofile(mp3path)
def ExtensionNameChangeover(filepath, ExtensionName):
name, suffix = os.path.splitext(filepath)
return name + "." + ExtensionName
def returnfliename(filepath):
return os.path.splitext(filepath)[0]
'''
class GetMp3Info:
"""获取歌曲信息"""
def __init__(self, path):
self.title = filename.split('.')[0] # 去掉文件名后缀
songFile = MP3(path)
self.getTitle(songFile, path)
self.getArtist(songFile)
self.getAlbum(songFile)
self.getLength(songFile)
def getTitle(self, songFile, path):
"""获取歌曲名
songFile:文件对象
path:文件地址
"""
try:
self.title = str(songFile.tags['TIT2'])
except:
filename = os.path.basename(path) # 从地址中获取文件名
def getArtist(self, songFile):
"""获取歌手名
songFile:文件对象
"""
try:
self.artist = str(songFile.tags['TPE1'])
except:
self.artist = ''
def getAlbum(self, songFile):
"""获取专辑名
songFile:文件对象
"""
try:
self.album = str(songFile.tags['TALB'])
except:
self.album = ''
def getLength(self, songFile):
timeLength = int(songFile.info.length)
mintime = timeLength // 60 # 转换为分钟
sectime = timeLength % 60 # 剩余的转换为秒
if sectime < 10:
sectime = '0' + str(sectime)
else:
sectime = str(sectime)
self.length = str(mintime) + ":" + sectime
'''
def getmusicimg(filepath):
inf = mutagen.File(filepath)
try:
artwork = inf.tags['APIC:'].data # 获取歌曲图片
except KeyError:
pass
title = inf.tags["TIT2"].text[0] # 获取歌曲名
path = title + '.tmp'
# 将图片保存为和歌曲同名,jpg格式的图片
try:
with open('./tmp/' + path, 'wb') as img:
img.write(artwork)
return './tmp/' + path
except UnboundLocalError:
pass