0

So i'm making a text based python file using inputs and if statements. But how do i play an mp3 file while the inputs are loading? I'm using Ubuntu btw

I have already tried pyglet, winsound, os but none of them work I've tried pygame but it doesn't play the file while loading the inputs


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

The only sucessfully atempt at playing the mp3 file was when i used pygame, but even then it wouldn't load the inputs at the same time Anyways, here's the code:

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)
Shadoww
  • 111
  • 1
  • 1
  • 6
  • you didn't show the code, that you used to trying play a sound. Do you have any code, that plays sound sucessfully (even if not in parallel with the loading)? You need probably some threading to play the mp3. What will be more challenging is to see how to stop playing the mp3. One way to do so would be to read find a library that plays mp3 from a pipe / stdin. and stop feeding it if the input is done. – gelonida Nov 08 '19 at 08:53

1 Answers1

1

Following code works for me:

But it's almost no change to the code that you posted.

I'm running on linux with python 3.6 and pygame 1.9.6.

If it doesn't work, then please specify OS, python version and pygame version.

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")
gelonida
  • 5,327
  • 2
  • 23
  • 41