Python Forum

Full Version: Trypng to use pygame mixer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to use PyGame mixer to play mp3 files, because it can be used without an api
to an online player, or without making a system call, (let me know other methods as I haven't
discovered then yet).
when I run some test code, (which requires BiblePaths.py from GitHub.com/Larz60p/Barroman1)
it fails to play the file, this because the program exits before it can play.
It works fine from interactive python session.

My question is how to wait until music finishes, or a better solution than pygame (must be able to run without internet or making system call)
Here's the test code:
import BiblePaths
from pygame import mixer
import json
import os


class PlayAudio:
    def __init__(self):
        self.bpath = BiblePaths.BiblePaths()

        with self.bpath.IndexedBible.open() as f:
            self.bible = json.load(f)

    def play_chapter(self, volume, book, chapter):
        mp3loc = self.bible[volume][book][chapter]['mp3path']
        mp3loc[1] = str(mp3loc[1]).replace(' ', '_')
        mp3path = self.bpath.KingJamesAudiopath / mp3loc[0] / mp3loc[1]
        newpath = (mp3path.resolve()).as_posix()

        mixer.init()
        mixer.music.load(newpath)
        mixer.music.play()

def testit():
    pa = PlayAudio()
    choice = 0
    volume = None
    book = None
    chapter = None

    while choice < 1 or choice > 2:
        try:
            choice = int(input('\nPlease choose:\n    1 = Old Testament\n    2 = New Testament\nYour choice: '))
        except ValueError:
            choice = 0

    if choice == 1:
        volume = 'Old Testament'
    else:
        volume = 'New Testament'

    book_list = list(pa.bible[volume].keys())
    book_count = len(book_list)

    while book not in book_list:
        os.system('cls||clear')
        print('choice:')
        for n, book in enumerate(book_list):
            print(f'    {n + 1:2} = {book}')
        try:
            choice = int(input('choice: '))
            book = book_list[choice-1]
        except ValueError:
            continue

    chapter = input('Please enter chapter: ')
    pa.play_chapter(volume, book, chapter)


if __name__ == '__main__':
    testit()
This interactive code works:
(venv) λ python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PlayAudio
>>> pa = Playudio.PlayAudio()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Playudio' is not defined
>>> pa = PlayAudio.PlayAudio()
>>> pa.play_chapter('Old Testament', 'Judges', '14')
While waiting for a reply, I think I'll take a look at the PyGame mixer code
What about mixer.get_busy()? https://www.pygame.org/docs/ref/mixer.ht...r.get_busy

Something like:
while True:
    if not pygame.mixer.get_busy():
        break
    time.sleep(0.1)
I actually use pygame.mixer in a console program at work (it watches for unread email, then plays a sound to let me know there's something new for me to look at), for the same reason, it seemed like the simplest way to just play a file without needing a ton of dependencies.
I'll take a look. I also just saw Snippsat's post (again, saw the first one but forgot that I did!)
I turned i back over to OP, with the PyGame.mixer which works, but once the module is called, there's
no stopping it (that I can find out) until finished, exiting program, or overriding with another mp3.
It turns out to be ok for this app, but a lot of baggage.

So I guess my work's not done yet!
(Mar-21-2018, 08:44 PM)Larz60+ Wrote: [ -> ]there's
no stopping it (that I can find out) until finished, exiting program, or overriding with another mp3.
pygame.mixer.stop() works for me.
https://www.pygame.org/docs/ref/mixer.ht...mixer.stop
OK. I didn't try that, too obvious! Thanks