gpt4 book ai didi

python - 无法使用pygame在循环内连续播放mp3文件?

转载 作者:行者123 更新时间:2023-12-04 15:30:25 25 4
gpt4 key购买 nike

<分区>

嗨,我想播放 mp3,但是当调用 logCreater 函数时,错误显示可以加载 mp3。第一次它可以正确播放音频,但是当它被召回时它无法加载 mp3。错误消息说 pygame.mixer.music.load 无法加载 xxxxx.mp3 文件实际上这是 lil 项目,这只是其中的一个模块。请建议我修改代码。

错误信息是:

追溯(最近的调用最后): 文件“e:\Tutorials etc\ProjBack\Healthy_programmer_cli\MainModule.py”,第 151 行,位于 时间计数() 文件“e:\Tutorials etc\ProjBack\Healthy_programmer_cli\MainModule.py”,第 65 行,在 timCount 中 EyeExcercise.logCreater() logCreater 中的文件“e:\Tutorials etc\ProjBack\Healthy_programmer_cli\EyeExcercise.py”,第 45 行 pygame.mixer.music.load("Eyesound.mp3")pygame.error: 无法打开“Eyesound.mp3”

import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
from os.path import expanduser

import time as t
import getpass
usernm = getpass.getuser()
from datetime import datetime
import pygame





def userDirFinder():
from os.path import expanduser
usrpth = expanduser("~")
mainp = os.path.join(usrpth, "Documents")
return mainp

def checknSetdir():
mainp=userDirFinder()
target_path = os.path.join(mainp,"HealthManger","Eye_Excercise_log")

if os.path.exists(target_path):
os.chdir(target_path)
else:

os.makedirs(target_path)
os.chdir(target_path)


def getCurrentDateandTime():
Dat = datetime.now()

currentD = Dat.strftime("%d/%m/%Y")
currentT = Dat.strftime("%I:%M %p")
return currentD , currentT



def logCreater():
print("Countdown paused")
pygame.mixer.init()
pygame.mixer.music.load("Eyesound.mp3")
pygame.mixer.music.play(-1)

write_msg = f"Eye Excercise Done by {usernm}"


while 1:

try:
print("Time for a Eye Excercise Break , After the Eye Excercise")
usr_msg = input("Type \"Done\" to stop this alarm: ")

usr_msg = usr_msg.lower()
if usr_msg != "done":
raise ValueError("Invalid Answer")
elif "done" == usr_msg:
checknSetdir()
with open("eye_excercise_log.txt","a") as fi:
cdat , ctim = getCurrentDateandTime()
fi.write(f"Date: {cdat} Time: {ctim} Message: {write_msg}\n")
# print("Log Created")

pygame.mixer.music.stop()

break


except Exception as e:
print(e)


def logReader():

try:
checknSetdir()

with open("eye_excercise_log.txt","r") as fi:
lis = fi.readlines()
for i in lis:
print(i)
input("Press to contiue")
except FileNotFoundError:
print("File is not created Yet")
input("Press to contiue")



if __name__ =="__main__":
while True:
logCreater()

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com