Mar-21-2018, 02:58 AM
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:
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