I would like to play all the mp3 files one by one in a directory using python, is there any module that will help me out to perform the task
Asked
Active
Viewed 2,405 times
3 Answers
0
You may want to look at the pygame
package. It lets you play MP3s.
Building off this related SO question, you could do something like
from time import sleep
from pathlib import Path # for directory listing
import pygame
DIRECTORY = Path('./path/to/your/directory')
mixer.init()
for fp in DIRECTORY.glob('*.mp3'):
# add each file to the queue
mixer.music.load(str(fp))
mixer.music.play()
# now wait until the song is over
while mixer.music.get_busy():
sleep(1) # wait 1 second
Note that the order of glob()
isn't controlled here, so you may want to introduce your own sorting/ordering.

Andrew F
- 2,690
- 1
- 14
- 25
-
Thank you for the code, but your snippets are throughing some errors. – Vineethvarma Vegesna Oct 16 '19 at 08:23
0
You can use the python ffmpeg (https://pypi.org/project/ffmpeg-python/)
use a loop to check for all files in the directory and use ffmpeg to play the mp3 file.
https://github.com/kkroening/ffmpeg-python/tree/master/examples

mail2subhajit
- 1,106
- 5
- 16
0
import os
import pygame
pygame.init()
pygame.mixer.init()
lists_of_songs = os.listdir("/path/to/the/directory")
for song in lists_of_songs:
if song.endswith(".mp3"):
file_path = "/path/to/the/directory/" + song
pygame.mixer.music.load(str(file_path))
pygame.mixer.music.play()
print("Playing::::: " + song)
while pygame.mixer.music.get_busy() == True:
continue

Vineethvarma Vegesna
- 73
- 6