鱼C论坛

 找回密码
 立即注册
查看: 1414|回复: 2

[作品展示] pygame播放音乐

[复制链接]
发表于 2018-3-22 20:49:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 南城顾她 于 2018-3-22 20:51 编辑

import pygame
from pygame.locals import *
import sys
from threading import Thread
import time

LRC = ''


class LrcWord(Thread):
    def __init__(self, content):
        super().__init__()
        self.content = content
        self.daemon = True

    def run(self):
        global LRC
        start_time = time.time()
        for word in self.content:
            word = word.split(']')
            if len(word) > 1:
                str_time = word[0].strip('[')
                minute, sec = str_time.split(':')
                time2 = float(minute) * 60 + float(sec)
                stop_time = time.time()
                time.sleep(time2 - (stop_time - start_time))
                LRC = word[1]


if __name__ == '__main__':
    f = open('赵照-你就是我最想要的丫头.lrc', 'r', encoding='gbk')
    content = f.readlines()
    Lrc = LrcWord(content)
    Lrc.start()
    pygame.init()
    size = width, height = 800, 538
    bg = (0, 0, 0)
    clock = pygame.time.Clock()
    END_EVENT = pygame.USEREVENT + 1
    pygame.mixer.music.set_endevent(END_EVENT)
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption('音乐播放器')
    image = pygame.image.load('赵照.jpg')
    image = pygame.transform.rotozoom(image, 0, 1.8)
    position = image.get_rect()
    music = pygame.mixer.music.load('赵照 - 你就是我最想要的丫头.mp3')
    pygame.mixer.music.play(1)
    font = pygame.font.SysFont('microsoftyaheimicrosoftyaheiui', 32)
    screen.fill(bg)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == END_EVENT:
                pygame.quit()
                sys.exit()
        info = font.render(LRC, True, (0, 255, 0))
        rect = info.get_rect()
        rect.center = (400, 500)
        screen.fill(bg)
        screen.blit(image, position)
        screen.blit(info, rect)
        pygame.display.flip()
        clock.tick(60)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-3-22 20:51:38 | 显示全部楼层
由于音乐文件不会上传所以不能播放吧  看看代码求优化
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-15 23:15:56 | 显示全部楼层
问一下楼主怎么解决延迟问题,我仅仅想播放一个音频,音调变了,时间还长了。。求解答
  1. import pygame

  2. pygame.mixer.init()

  3. pygame.mixer.music.load('Pikachu (50).wav')
  4. pygame.mixer.music.play()

  5. while True:
  6.     #pygame.time.delay(60)
  7.     if pygame.mixer.music.get_busy() == False:
  8.         break
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 18:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表