playing mp3 files in Python

Asked

Viewed 9,733 times

-2

I was doing Video Course exercises on Python and I stagnated on Challenge 21, whose goal is to make a program that touches an mp3 file. However, I have tried as shown in the video and it did not work, I have tried the ways I saw in the comments and it also did not work because they are either obsolete or in maintenance. I would like to know about any current code that effectively performs its function. I thank you from now on. :)

Just follow my codes I tried before:

Through the Pygame of the Year video (2017)

import pygame
pygame.mixer.init()
pygame.init()
pygame.mixer.music.load('McPoze.mp3')
pygame.music.play()
pygame.event.wait()

-Error: pygame.error: Couldn’t open 'Mcpoze.mp3'

For help of comments

import pygame
pygame.mixer.init()
pygame.mixer.music.load('McPoze.mp3')
pygame.mixer.music.play()
input()
pygame.event.wait()

-Error: pygame.error: Couldn’t open 'Mcpoze.mp3'

import playsound
playsound.playsound('McPoze.mp3')

-Error: Unicodedecodeerror: 'utf-8' codec can’t Decode byte 0xe3 in position 24: invalid continuation byte

import webbrowser
webbrowser.open('McPoze.mp3')

-Error: Opens a tab in Internet Explorer unrelated to mp3

  • Friend this happens because your Python is updated and the library is not. I recommend using Python3.6 to use the Pygame library.

5 answers

0

import pygame

pygame.init()
pygame.mixer.music.load('favela.mp3')
pygame.mixer.music.play()
pygame.event.wait()
input()
  • 1

    I recommend inserting explanations about what was done in your answer in order to not only "solve" the problem of the author of the question, but clarify what was done and how it resulted in the resolution of the reported problem.

-1

It seems that the tool was "discontinued", but I was able to use the following. Detail: I’m using Pycharm 2020 so I’ll tell you how I did for him ok. Go to File, Settings, python interpreter, double click on Pip, then in the search bar write playsound, on the screen on the right has to appear something that this library is available from Taylor Marks github. In the lower left corner mark the box written: Install to user’s site Packages directory, then just click the button below Install Package. Just wait for the installation and then send the code. Mine gave good! como fazer

-1


The code you used with the pygame should work.

Your error may not be related to pygame. The file McPoze.mp3 exists and is in the same folder where interpreter was opened? You can check the current python interpreter directory with

import os
print(os.getcwd()) 

If the folder is not the same where the file is McPoze.mp3 then just put the full file path instead of just the name.

There are also some other libraries that you could try as an alternative to play audio in python. That link list a few options. I quickly tested the first two (playsound and pydub installed using Pip) and worked well. I reproduce the example of the two I tested below.

from playsound import playsound
playsound('audio.mp3')

The command playsound locks while audio plays and Ctrl+C does not stop immediately.

from pydub import AudioSegment
from pydub.playback import play

song = AudioSegment.from_mp3("sound.wav")
play(song)

The pydub is more interassante. It shows the progress of the audio and Ctrl+C oara immediately in a clean way.

  • The function playsound continues to give the same error that I had put up and in relation to the pydub that you objectified then, I did not understand how to use it even entering the attenuated site. :(

  • Maybe because it didn’t matter AudioSegment. I edited the answer and added import.

  • Your problem may also be just because the current python interpreter folder is not the same as the mp3 file. I edited the answer on how to check if this is the case.

  • The problem was with the same file location, thank you very much friend! I managed to solve with the module to find the directory! :)

-1

I just had the same problem and, analyzing the library pygame, I realized that function music was replaced by mixer_music. With this last one, everything works OK.

My example:

import pygame

pygame.mixer.init()
pygame.init()
pygame.mixer.music.load('alok_ringtone.mp3')
pygame.mixer_music.play()
pygame.event.wait()

I hope I’ve helped.

-2

import pygame
import os
pygame.init()
if os.path.exists('musica.mp3'):
  pygame.mixer.music.load('musica.mp3')
  pygame.mixer.music.play()
  pygame.mixer.music.set_volume(1)

  clock = pygame.time.Clock()
  clock.tick(10)

  while pygame.mixer.music.get_busy():
     pygame.event.poll()
     clock.tick(10)
else:
  print('O arquivo musica.mp3 não está no diretório do script Python')

Browser other questions tagged

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