3.19更新:
import sys
import pygame
from pygame.locals import *
import tool
pygame.init()
pygame.mixer.init()
playstatus = 'stop'
mouseststue = "up"
filepath = "example.mp3"
loudup_x = 400
try:
with open('./tmp/lastmp3filepath', 'r') as f:
filepath = f.read()
except:
filepath = 'example.mp3'
try:
with open('./tmp/lastmp3filevol', 'r') as b:
loudup_x = int(b.read())
except:
loudup_x = 400
def quit():
global loudup_x
try:
pygame.quit()
except pygame.error:
pass
with open('./tmp/lastmp3filepath', 'w') as F:
F.write(filepath)
with open('./tmp/lastmp3filevol', 'w') as B:
B.write(str(loudup_x))
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)
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():
setloud = False
global filepath, playstatus, screen, mouseststue, loudup_x
play = Player
clock = pygame.time.Clock()
statusbar = pygame.image.load(r'./lib/statusbar.bmp')
ps = pygame.image.load(r'./lib/ps.bmp').convert_alpha()
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".\lib\FZPTYJW.TTF", 40)
tipa = tip1.render('将文件拖拽至窗口,就可以播放音乐了', True, (0, 0, 255))
try:
musicimg = pygame.image.load(tool.getmusicimg(filepath))
except:
musicimg = pygame.image.load('./lib/playimg.bmp')
while True:
musicloud = loudup_x - 100
# stylist = pygame.key.get_pressed()
mouse_x, mouse_y = pygame.mouse.get_pos()
screen.fill((100, 200, 100))
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:
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, [350, 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()
clock.tick(20)
play.set_volume(musicloud / 800)
if __name__ == '__main__':
main()