Using a library called pygame When creating a program like a so-called timer that plays music after a specified time has passed There was a problem that no error message was displayed and no music was played. I will write about the solution.
Python 3.6.8 pygame 1.9.6
When music is output by pygame.mixer.music.play ()
Music can now be played by delaying the execution of the program by the time the music is played with time.sleep ()
.
#Create a program to play music
import pygame
import time
react_time = int(input("Set how many seconds you want the music to play>>"))
print(react_time)
time.sleep(react_time)
pygame.mixer.init()
pygame.mixer.music.load("BGM.mp3")
pygame.mixer.music.play()
print("music play")
When you execute the code and enter 3 in the terminal,
Set how many seconds you want the music to play>>3
3
music play
Only the result is output, and no music is played.
By causing a delay with time.sleep ()
after pygame.mixer.music.play ()
,
Music started to play.
import pygame
import time
react_time = int(input("Set how many seconds you want the music to play>>"))
print(react_time)
time.sleep(react_time)
pygame.mixer.init()
pygame.mixer.music.load("BGM.mp3")
pygame.mixer.music.play()
time.sleep(10) # time.sleep()To secure the time until the end of processing with
print("music play")
No sound in python pygame https://teratail.com/questions/173534
Recommended Posts