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

How to play an mp3 file while a text based program is loading? Python

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

我已经尝试过 pyglet、winsound、os,但其中 none 有效 我试过 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)

以下代码适用于我:

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

我 运行 linux python 3.6 和 pygame 1.9.6。

如果不起作用,请指定 OS、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")