Python Forum

Full Version: Pygame.mixer.music.load(...) not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, first time using pygame and relatively new with Python. pygame.mixer.music.load(listofsongs[0]) causes the program to not respond and cpu goes to ~100%. I would assume it's stuck in an endless loop but I'm not sure why. Anybody know why this might be?

Also if I comment out the pygame lines the directory window doesn't close...

Thanks in advance.

Here's a snippet:
def directorychooser():

    directory = tkFileDialog.askdirectory()
    os.chdir(directory)

    for files in os.listdir(directory):
        if files.endswith(".ogg"):
            listofsongs.append(files)


    pygame.mixer.init()
    pygame.mixer.music.load(listofsongs[0])
    pygame.mixer.music.play()

directorychooser()
What pygame version are you using ?
import pygame
print(pygame.__file__)
What os are you using ?
Window 7 - 10, Linux, Mac
Pygame version 1.9.3

Mac 10.13.2

Python 2.7.14
My advice.
1. make sure you have all libraries install .

Sounds like you may have a bug version of pygame.
I had the same problem on Antergos Linux (Arch Linux)
I compile my from source. But you always can grab another version.

There are many post on pygame and Mac issue.
Metulburr even made a post in Tutorials about pygame issues.
Pygame Issue
Quote:directory = tkFileDialog.askdirectory()
Never mix pygame and tkinter. This would be my assumption of the reason your CPU is going AWAC.
His example works on my computer with a couple of changes.
python 3 code here.
from tkinter import filedialog
import pygame
import os

def directorychooser():

    directory = filedialog.askdirectory()
    os.chdir(directory)

    listofsongs = []
    for files in os.listdir(directory):
        if files.endswith(".mp3"):
            listofsongs.append(files)


    pygame.mixer.init()
    pygame.mixer.music.load("path to music" + listofsongs[0])
    pygame.mixer.music.play()

    pygame.time.delay(2000)

directorychooser()

Better line. I should have use.
pygame.mixer.music.load(os.path.join(directory, listofsongs[0]))