我使用了之前的堆栈溢出帖子来帮助我开发以下代码:
import os
import pygame
from pygame import *
import sys
GAME_FOLDER = os.path.dirname(__file__)
IMG_FOLDER = os.path.join(GAME_FOLDER, "img")
from time import sleep
pygame.mixer.init()
pygame.mixer.music.load(os.path.join(IMG_FOLDER, 'Noota.mp3'))
print("p")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
sleep(1)
print ("done")尽管代码加载时没有显示错误消息,但它并没有播放音乐;相反,我听到的只是“滴答声”(大约每秒一个滴答声)。我想知道是否有人知道为什么会发生这种情况,以及我如何才能修复它。
作为参考,我在mackintosh计算机上使用python 3,当使用媒体播放器加载音乐文件时,它可以在计算机上播放。
谢谢!
发布于 2017-11-30 00:10:26
首先,您可以尝试将音频文件加载为pygame.mixer.Sound,并使用SoundName.play()检查是否不是mixer.load()损坏,如果仍然发生,则可能是音频文件有问题,请使用通用的最新格式,如.ogg,这样它就可以在所有平台上工作
此外,time.sleep()可能会扰乱声音,因为睡眠会停止所有功能,在这种情况下可能会停止声音。如果你正在尝试声音方法,这里有一个例子:
import os
import pygame
from pygame import *
import sys
GAME_FOLDER = os.path.dirname(__file__)
IMG_FOLDER = os.path.join(GAME_FOLDER, "img")
from time import sleep
noota = pygame.mixer.sound(os.path.join(IMG_FOLDER, 'Noota.mp3'))
print("p")
noota.play()
# Maybe the music is being cut off here v
while pygame.mixer.music.get_busy():
sleep(1)
print ("done")https://stackoverflow.com/questions/47555614
复制相似问题