第二天更新(已经可以播放音乐了)

import sys
import time
import pygame
from pygame.locals import *
import tool

pygame.init()
pygame.mixer.init()


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():
        try:
            pygame.mixer.music.play()
        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 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("title")
screen = pygame.display.set_mode((1000, 700), 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():
    global filepath
    play = Player
    statusbar = pygame.image.load(r'./lib/statusbar.bmp')
    ps = pygame.image.load(r'./lib/ps.bmp').convert_alpha()
    tip1 = pygame.font.Font(r"C:\Users\GBX08\PycharmProjects\musicplayer\lib\FZPTYJW.TTF", 40)
    tipa = tip1.render('将文件拖拽至窗口,就可以播放音乐了', True, (0, 0, 255))

    while True:
        # 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
                play.load(filepath)
                play.play()
                play.set_volume(0.2)
            if event.type == pygame.MOUSEBUTTONDOWN:
                if mouse_x < 200 and mouse_y < 100:
                    play.load(filepath)
                    play.play()
                    play.set_volume(0.2)

        screen.blit(statusbar, [0, 0])
        screen.blit(tipa, [250,100])

        if mouse_x < 200:
            blit_alpha(screen, ps, [0, mouse_y - mouse_y % 100], 100)
        pygame.display.flip()


if __name__ == '__main__':
    main()

创建了一个lib文件夹

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值