加载基于文本的程序时如何播放mp3文件?蟒蛇

所以我正在使用输入和if语句制作基于文本的python文件。 但是如何在输入加载时播放mp3文件? 我正在使用Ubuntu btw

我已经尝试过pyglet,winsound,os,但它们都不起作用 我尝试了pygame,但在加载输入时无法播放文件


print("Welcome user")
name = input("Client name: ")
gender = input("Mr or Miss: ")
age = input("Client age: ")
room = input("Room: ")
sure = input("""All done!!!
Press any key to show the view!""")

welcome = f"""Welcome to room {room} {gender}. {name}!
Have a nice stay"""

if sure == "a":
    print(welcome)
else:
    print(welcome)

Os - "Module os has no startfile member"

pyglet - Doesnt import

winsound - Doesn't play the file

在播放mp3文件时唯一成功的尝试是当我使用pygame时,但是即使那样,它也不会同时加载输入 无论如何,这是代码:

import pygame
import time
pygame.init()

pygame.mixer.music.load("elevmusic.mp3")

pygame.mixer.music.play()

time.sleep(10)

print("Welcome user")
name = input("Client name: ")
gender = input("Mr or Miss: ")
age = input("Client age: ")
room = input("Room: ")
sure = input("""All done!!!
Press any key to show the view!""")

welcome = f"""Welcome to room {room} {gender}. {name}!
Have a nice stay"""

if sure == "a":
    print(welcome)
else:
    print(welcome)
caowei5323013 回答:加载基于文本的程序时如何播放mp3文件?蟒蛇

以下代码对我有用:

但是您发布的代码几乎没有变化。

我正在使用python 3.6和pygame 1.9.6在Linux上运行。

如果它不起作用,请指定操作系统,python版本和pygame版本。

import pygame
import time

pygame.init()

pygame.mixer.music.load("elevmusic.mp3")
print("loaded")

pygame.mixer.music.play(loops=-1)  # repeat indefinitely
print("started play")

print("Welcome user")
name = input("Client name: ")
gender = input("Mr or Miss: ")
age = input("Client age: ")
room = input("Room: ")
sure = input("""All done!!!
Press any key to show the view!""")

welcome = f"""Welcome to room {room} {gender}. {name}!
Have a nice stay"""

pygame.mixer.music.stop()
# pygame.mixer.music.fadeout(1000)  # or use fadeout 
if pygame.version.vernum >= (2,0):
    # free some resources. but this exists only for newer
    # versions of pygame
    pygame.mixer.music.unload()

if sure == "a":
    print(welcome)
else:
    print(welcome)

print("now simulating some activity without music")
time.sleep(10)
print("program ends")
本文链接:https://www.f2er.com/3139402.html

大家都在问