[英]pygame cant play mp3 after grabbing a screenshot
我有一些代码让 PyGame 播放 mp3。 但我截屏后无法播放mp3
文件_A.py
from pygame import mixer
mixer.init()
def play(PATH, target):
# play the sound
print("play", target + ".mp3")
mixer.music.load(PATH + target + ".mp3")
mixer.music.play()
play("x","y") // its work if i call here
文件_B.py
from .file_A import play
import pyscreenshot as ImageGrab
def main():
play("x1","y") // its working here, but
left = ImageGrab.grab(bbox=(1,2,3,4)) // after grab
play("x2","y") // log printed that method called but sound not working
python 3.7.4
pygame 1.9.6
pyscreenshot 1.0.0
假设文件在同一目录中,我唯一的假设可能是当该函数导入另一个文件时,它可能会遇到路径问题,因此它可能无法播放该文件。
我也有点困惑,你的路径被称为"xy.mp3"
。 您可以尝试给它一个绝对路径,例如
将您的函数定义更改为以下内容并尝试从file_B.py
调用它
def play():
# play the sound
print("play", target + ".mp3")
mixer.music.load("c:/programs/mygame/music/8bit-jingle-twist.mp3")
mixer.music.play()
此外,考虑将这一行pygame.mixer.music.stop()
添加到播放函数,以确保在获取下一个实例播放实例之前正确终止使用中的音乐频道。
我发现了主要问题,它不是错误。 但是音频中断,因为该方法中的所有代码都已经执行,并且在混音器播放音频之前完成的方法
文件_B.py
from .file_A import play
import pyscreenshot as ImageGrab
import time
def main():
play("x1","y") // its working here, but
left = ImageGrab.grab(bbox=(1,2,3,4)) // after grab
play("x2","y") // log printed that method called but sound not working
time.sleep(n) // wait the audio play
或者
在mixer.play之后在file_a.py上添加这个
while mixer.music.get_busy():
time.Clock().tick(10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.