Playing mp3 music in Python

Asked

Viewed 580 times

-1

all right? Well I come here to ask for a solution regarding playing mp3 music in Python. I imported the Pygame library and used the mixer module to play python music:

import pygame
pygame.init()
pygame.mixer.music.load('brain_damage.mp3')
pygame.mixer.music.play(loops=0, start=0.0)
pygame.event.wait()

After I write this script and run my program, it gives an error output, saying:

Traceback (most recent call last):
File "/home/gabriel/Documentos/GitHub/Projetos Pessoais/olamundo.py/_exercicios/teste.py", line 3, in <module>

pygame.mixer.music.load('music/brain_damage.mp3')
pygame.error: Couldn't open 'music/brain_damage.mp3'

Esse é o arquivo Python que fica dando erro

For comparison purposes, I created another Python file and used the same code as the image above, and to my surprise, the mixer managed to run my mp3 file. You know why you proceed? Segundo print com o programa rodando

  • you checked if the song is in the same folder as the code?

  • yes, it’s in the same code folder

  • mp3 file is not in "music" folder, in this folder are just few.wav files. The song I want to play in my program is in the folder called "_exercicios" along with the code

  • What’s this OS you’re wearing?

1 answer

1

If mp3 is in the same script folder:

from pygame import mixer 

mixer.init()
mixer.music.load('brain_damage.mp3')
mixer.music.play()

If the mp3 folder is at the same level as the script you add a .:

from pygame import mixer 

mixer.init()
mixer.music.load('./teste/brain_damage.mp3')
mixer.music.play()

If the folder is one level above the script you add two .

from pygame import mixer 

mixer.init()
mixer.music.load('../teste/brain_damage.mp3')
mixer.music.play()

You can put the full path in the load, example: F:/stackoverflow/teste/brain_damage.mp3

The way teste is fictitious, you should check the path to the file in question.

  • Opa vlw ai glr, I managed to fix this bug. It was really the level of the folder!

  • 1

    as I put the songs in a separate folder, I inserted the ".." two point, and my scripts worked. THANK YOU VERY MUCH!

  • Gabriel, good afternoon! Nothing! Glad you solved the problem! Hugs!

  • 1

    If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you prefer the other answer, you can mark it as correct/accepted, but only one response can be marked that way. If you still have any questions or would like further clarification, feel free to comment.

Browser other questions tagged

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