Playing mp3 song on python
Try this. It's simplistic, but probably not the best method.
from pygame import mixer # Load the popular external library
mixer.init()
mixer.music.load('e:/LOCAL/Betrayer/Metalik Klinik1-Anak Sekolah.mp3')
mixer.music.play()
Please note that pygame's support for MP3
is limited. Also, as pointed out by Samy Bencherif, there won't be any silly pygame window popup when you run the above code.
Installation is simple -
$pip install pygame
Update:
Above code will only play the music if ran interactively, since the play()
call will execute instantaneously and the script will exit. To avoid this, you could instead use the following to wait for the music to finish playing and then exit the program, when running the code as a script.
import time
from pygame import mixer
mixer.init()
mixer.music.load("/file/path/mymusic.ogg")
mixer.music.play()
while mixer.music.get_busy(): # wait for music to finish playing
time.sleep(1)
Playing an MP3 file using Python
You can try the playsound
module
from playsound import playsound
playsound("audio_file.mp3")
How to Play a Mp3 file using Python in Windows?
Try using this (solution taken from here):
mixer.music.load('C:\Users\sharathchandra\Downloads\17.wav') # you may use .mp3 but support is limited
mixer.music.play()
Many more ways can be found here and here.
Also, you might want to read the documentation on Pygame's music.
How can I play audio (playsound) in the background of a Python script?
In Windows:
Use winsound.SND_ASYNC to play them asynchronously:
import winsound
winsound.PlaySound("filename", winsound.SND_ASYNC | winsound.SND_ALIAS )
To stop playing
winsound.PlaySound(None, winsound.SND_ASYNC)
On Mac or other platforms:
You can try this Pygame/SDL
pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()
Related Topics
Authenticate from Linux to Windows SQL Server with Pyodbc
Basic Python Hello World Program Syntax Error
How to Avoid "Runtimeerror: Dictionary Changed Size During Iteration" Error
How to Load All Entries in an Infinite Scroll at Once to Parse the HTML in Python
Python Ta-Lib Install Error, How Solve It
Conda Command Will Prompt Error: "Bad Interpreter: No Such File or Directory"
Appending the Same String to a List of Strings in Python
How to Use the Same Python Virtualenv on Both Windows and Linux
Pickled File Won't Load on MAC/Linux
Pelican 3.3 Pelican-Quickstart Error "Valueerror: Unknown Locale: Utf-8"
How to Create a Namespace Package in Python
Convert HTML Entities to Unicode and Vice Versa
Why Does Pyimport_Import Fail to Load a Module from the Current Directory
Too Many Values to Unpack', Iterating Over a Dict. Key=>String, Value=>List
How to Exit Linux Terminal Using Python Script
Python Linux Dmidecode, How to Obtain Hw Info by Parsing