鱼cpython学习者 发表于 2020-9-5 12:08:07

pygame打不开音频

本帖最后由 鱼cpython学习者 于 2020-9-6 11:00 编辑

想做一个爬取百度翻译的单词音频,然后自动播放音频的爬虫,想来想去觉得用pygame播放音频比较合适,结果pygame打不开我的音频
import requests
import json
import os
import pygame
import sys

os.chdir(r'C:\Users\fengu\Desktop\test')
pygame.init()
pygame.mixer.init()

def save_music():
        text = input('>>>')
        data = {'query': text}
        response = requests.post('https://fanyi.baidu.com/langdetect', data = data, headers = headers)
        lan = json.loads(response.text)['lan']
        url = 'https://fanyi.baidu.com/gettts?lan={0}&text={1}&spd=3&source=web'.format(lan, text)
        response = requests.get(url, headers = headers)
        with open('music.mp3', 'wb') as f:
                f.write(response.content)


save_music()
size = width, height = 1, 1
bg = (255, 255, 255)
screen = pygame.display.set_mode(size)
while True:
        for event in pygame.event.get():
                if event.type == 'QUIT':
                        sys.exit()
        screen.fill(bg)
        pygame.display.flip()
        test = pygame.mixer.Sound('music.wav')
        test.set_volume(0.2)
        test.play()



Traceback (most recent call last):
File "C:/MyPythonProjects/WebSpideFile/readword.py", line 33, in <module>
    test = pygame.mixer.Sound(r'C:\Users\fengu\Desktop\test\music.wav')
pygame.error: Unable to open file 'C:\\Users\\fengu\\Desktop\\test\\music.wav'


页: [1]
查看完整版本: pygame打不开音频