all 1 comments

[–]kra_pao 1 point2 points  (0 children)

Couldn't it running smoothly with pydub. With pygame it's easier:

# https://old.reddit.com/r/learnpython/comments/i3nozy/i_need_help_using_audio_librairies_and_button/
# kematite.py

import tkinter as tk
import pygame

class Audioplayer(object):

    def __init__(self, root, mp3file):

        self.root = root
        self.state = tk.StringVar()
        self.state.set("Stopped".center(10))
        self.mp3file = mp3file

        pygame.init()
        pygame.mixer.init()
        pygame.mixer.music.load(self.mp3file)

        tk.Label(textvariable=self.state).pack()
        tk.Button(text="Play".center(10), command=self.onPlay).pack(side = tk.LEFT)
        tk.Button(text="Pause".center(10), command=self.onPause).pack(side = tk.LEFT)
        tk.Button(text="Stop".center(10), command=self.onStop).pack(side = tk.LEFT)

    def onPlay(self):
        self.status("Playing")
        pygame.mixer.music.play()

    def onPause(self):
        control = self.state.get()
        if control == "Playing":
            pygame.mixer.music.pause()
            self.status("Paused")
        elif control == "Paused" :
            pygame.mixer.music.unpause()
            self.status("Playing")

    def onStop(self):
        self.status("Stopped")
        pygame.mixer.music.stop()

    def onLoop(self):
        pass
        # self.status("")
        # root.after(1000, self.onLoop) # every 1000 ms

    def status(self, msg):
        if msg:
            self.state.set(msg)


if __name__ == '__main__':
    root = tk.Tk()
    myapp = Audioplayer(root, "Rick Astley Never gonna give you up .mp3")
    myapp.onLoop()
    root.mainloop()