Python Forum
Pygame.mixer.music.load(...) not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame.mixer.music.load(...) not working
#1
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()
Reply
#2
What pygame version are you using ?
import pygame
print(pygame.__file__)
What os are you using ?
Window 7 - 10, Linux, Mac
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Pygame version 1.9.3

Mac 10.13.2

Python 2.7.14
Reply
#4
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
99 percent of computer problems exists between chair and keyboard.
Reply
#5
Quote:directory = tkFileDialog.askdirectory()
Never mix pygame and tkinter. This would be my assumption of the reason your CPU is going AWAC.
Recommended Tutorials:
Reply
#6
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]))
99 percent of computer problems exists between chair and keyboard.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020