Search code examples
python-3.xpygamemacos-mojave

How can I shuffle a folder of music without interupting or pausing other code in python?


In short I would like to shuffle a folder of .mp3 files in the background so I can still have a program running. I on Mac OS X. I have managed to get all the files in a list using Glob and I can then play them with the playsound plugin found here. However this script prevents anything else from running until all the songs are finished with isn't what I want. I tried pygame with this code... pip install pygame (Which worked successfully), and then...

import pygame
pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()

This however always returns the same error, pygame.error: MPEG file does not have any audio stream. So I have the files in a list but no way to play them in the background. Can someone help with this please?

UPDATE ON CODE:

I actually managed to get it to play sounds with this code bellow...

import time
import glob
import pygame

pygame.mixer.init()
def play_my_mix(data = "error"):
    for file in glob.glob("data/music/mix/*.mp3"):
        pygame.mixer.music.load(file)
        pygame.mixer.music.play()

print("test")
play_my_mix()
print("apple")
time.sleep(15)

However the audio is in awful quality. It sounds sort of warped and slow. The time.sleep(15) line is there to keep the program running because otherwise it just stops which is ok considering this is a test. I did however change that to this...

x = 1
while x == 1:
    print("good")

In case it was something to do with the time but it didn't change it at all.


Solution

  • So I fixed the first part with the update code as mentioned in the edit. The second problem with the audio lag I fixed by changing pygame.mixer.init() to pygame.mixer.init(44100, -16, 2, 2048)