How can I play music in Python? Without PyGame! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

How can I play music in Python? Without PyGame!

¿Cómo puedo reproducir música en Python? ¡Sin PyGame!

24th Nov 2021, 11:54 PM
CGO!
CGO! - avatar
12 Answers
+ 6
I ran this script on my android phone in pydroid 3 and it works for sure, I just played a song. NO WINDOW so don't complain about pygame it's the only one I've found that works import os import time from pygame import mixer DIR = "/storage/emulated/0/Download/" os.chdir(DIR) mixer.init() mixer.music.load("Ayo Technology.mp3") mixer.music.play() while mixer.music.get_busy(): # wait for music to finish playing time.sleep(1) EDIT: And for the geniuses with a problem with this answer, the parameter of mixer.music.load() is a personal file, NOT just a plug and play. Put a song in the downloads file and use THAT name you gods of code. And mabey, try it out first.
25th Nov 2021, 12:46 AM
Slick
Slick - avatar
+ 6
pip install playsound==1.2.2 from playsound import playsound playsound('music_here.wav') playsound('music_there.mp3', True) OR with Qt pip install python-vlc import vlc vlc.MediaPlayer("music_everywhere.mp3").play()
25th Nov 2021, 12:01 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 6
What do you mean? Playsound is a single function module. You can only interrupt or suspend it, e.g. by using the multiprocessing and psutil modules: import multiprocessing as mp from playsound import playsound import psutil class Player: def __init__(self, filepath ): self.filepath = filepath def play(self): playsound(self.filepath,True) if __name__ == "__main__": mp.set_start_method('spawn') import multiprocessing as m pause = False player = Player('music.mp3') process = mp.Process(target=player.play) process.start() while process.is_alive(): input() pause = not pause if pause: psutil.Process(process.pid).suspend() else: psutil.Process(process.pid).resume() Big plus of the playsound - it doesn't have any dependencies. If you need GUI, then you can use tkSnack(then you need Tkinter) If you need a player, look towards this cool project, for example. https://github.com/alvin-the-programmer/snek
25th Nov 2021, 3:07 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 4
https://docs.python.org/3/library/mm.html Here is the documentation for the standard libraries in python that you can use to play audio.
24th Nov 2021, 11:59 PM
William Owens
William Owens - avatar
+ 3
Thanks to all. But, Alexey Kopyshev , how I can upgrade the system?
25th Nov 2021, 1:11 AM
CGO!
CGO! - avatar
+ 3
Alexey Kopyshev Pydroid say I need install pygobject and my pip need be upgraded
25th Nov 2021, 12:40 PM
CGO!
CGO! - avatar
+ 2
You probably installed version 1.3.0 by default. Uninstall it. You need 1.2.2 just open terminal in pydroid and type: pip install pydroid==1.2.2
25th Nov 2021, 12:49 PM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 2
Hmm... for pygobject you need pycairo. Pip you can easily upgrade: pip install --upgrade pip
25th Nov 2021, 6:35 PM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 2
Alexey Kopyshev Thank you, but now I can't install pycairo, I install pyproject
25th Nov 2021, 8:18 PM
CGO!
CGO! - avatar
+ 2
I use termux on Android for Python and Pydroid only as editor. Maybe you should also try it. Here's the full instruction how you can setup termux https://www.fatalerrors.org/a/basic-use-of-termux.html
26th Nov 2021, 5:05 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 2
import playsound playsound("Filepath")
26th Nov 2021, 9:54 AM
Saad Khan
Saad Khan - avatar
+ 2
Slick , sorry about my thumb down. At the moment for Pydroid, your option is the only one that works. They never turned on pycairo support, even though they've been asking for it for a long time. All the other options bring with them a GUI.
26th Nov 2021, 6:21 PM
Alexey Kopyshev
Alexey Kopyshev - avatar