music does not play

Asked

Viewed 2,884 times

1

I have the following python code

#*-coding:utf-8;-*
import pygame
pygame.init()
pygame.mixer.music.load('ex1.mp3')
pygame.mixer.music.play()
pygame.event.wait()

In this case, it should play a song while running on pycharm, however, it performs the function, waits a few seconds and simply terminates the program, without returning any errors, how can I fix this?

By using it this way

#*-coding:utf-8;-*
from pygame import mixer
mixer.init()
mixer.music.load('ex1.mp3')
mixer.music.play()
import time
time.sleep(360)

The code ran normally, but I don’t think it’s the best way to do it with python/pygame

6 answers

3


Try this:

from pygame import mixer
mixer.init()
mixer.music.load('ex1.mp3')
mixer.music.play()
x = input('Digite algo para parar...')
  • still doesn’t seem like a good alternative

  • worked what I proposed?

  • works, but it makes me unable to perform anything else with the music, and also, I don’t think it’s a good method, so I didn’t respond

3

The point is that the call pygame.event.wait() in almost all cases will return immediately: it returns whichever pygame event - including mouse drive, etc...

If you want to run code in parallel with the music playing, simply write this code pygame.mixer.music.play() starts the music asynchronously, parallel to its Python code.

If you use time.Leep, your Python code does nothing and Fic music is tied. The call to event.wait() returns, and the program is terminated - hence the difference between its code. If you have a main loop of your application, waiting for keyboard events, doing things, etc...this will work while the music plays.

(Parallel tip: running the program from inside pycharm is just a pycharm facility while developing - it is important to understand that your program exists and can be called directly by the operating system. Python programs do not run "inside pycharm". )

  • I didn’t understand the question of the asynchronous method, I’m still beginner in python and I don’t know how to use this method

2

import pygame
pygame.mixer.init()
pygame.mixer.music.load('mu.mp3')
pygame.mixer.music.play()
x = input('Digite algo para parar a musica...')

Shortening the code

from pygame import mixer
mixer.init()
mixer.music.load('mu.mp3')
mixer.music.play()
x = input('Digite algo para parar a musica...')

Instead put the code the way you put it and it will work... as Victor Antonio said, the call to event.wait() returns and the program is terminated...

1

One option is to use a repeat loop next to the control if the mixer.music is busy (method get_busy).

In the example I am repeating the song until the variable i is greater than 5.

from pygame import mixer
import time

mixer.init()
mixer.music.load('ex1.mp3')
mixer.music.play(-1)
i = 0
while (mixer.music.get_busy()):
  # Executa o que você quiser executar, no exemplo estou executando somente um sleep e um print
  # Fiz uma condição para quando o i > 5 parar de tocar a música e parar a execução por consequência
  time.sleep(1)
  if i > 5:
    mixer.music.stop()

1

The first method is not working because it should be pygame.mixer.init() instead of pygame.init()

  • I’ve been reading the documentation of Pygame and saw that logic has foundation but the suggested function is another. It is written "Some platforms require the module pygame.mixer, is initialized after initialization of the display modules. At the top level pygame.init() takes care of it automatically, but can not pass any argument to the init of the mixer. To solve this, the mixer has a function pygame.mixer.pre_init()to set proper standards before higher level init is used."

1

I had this same problem using pygame and managed to fix it by creating a window through pygame.display.set_mode. For me at least, the mixer only worked after that. Try this code:

import pygame
pygame.init()
w_geometry = (300,300)
pygame.display.set_mode(w_geometry)
pygame.mixer.music.load('ex1.mp3')
pygame.mixer.music.play()

Tip: It is very cool also that you use pygame.mixer.init passing the size of buffer so there is no delay. The smaller the buffer size, the less the delay. Example:

import pygame
pygame.init.mixer(buffer=128)
pygame.init()
w_geometry = (300,300)
pygame.display.set_mode(w_geometry)
pygame.mixer.music.load('ex1.mp3')
pygame.mixer.music.play()
  • 1

    It would look better as a comment.

  • Okay, see if it’s better now. I put as a response and not as a comment because when I started here at Stackoverflow there was a message saying that any and all answers should be put as a response. Already questions and others should be put in comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.