0

I'm a complete beginner in Python and currently making a GUI with Tkinter that can play mp3 files. just for practice.

I'm using a Mac, and the rainbow wheel that appears when a program lags shows up when I press the play button I made. And it doesn't let me press any buttons while the mp3 file is playing.

Can anybody help me figure this out please?

from pydub import AudioSegment
from pydub.playback import play
from tkinter import *

class MP3:

def __init__(self, master):
    frame = Frame(master)
    frame.pack()

    self.go_back_button = Button(frame, text = '<<')
    self.go_back_button.grid(row = 0 , column = 0)

    self.play_button = Button(frame, text = '|>', command = self.play_song)
    self.play_button.grid(row = 0 , column = 1)

    self.pause_button = Button(frame, text = '||', command = self.pause_song)
    self.pause_button.grid(row = 0 , column = 2)

    self.go_forward_button = Button(frame, text = '>>')
    self.go_forward_button.grid(row = 0 , column = 3)

    self.shuffle_button = Button(frame, text = 'SHUFFLE')
    self.shuffle_button.grid(row = 0 , column = 4)

    self.is_paused = False

    self.song_list = ['songs.mp3']
    self.i = 0


def play_song(self):
    while self.is_paused is False:
        song = AudioSegment.from_mp3("/Users/bang/Desktop/music/{}".format(self.song_list[self.i]))
        play(song)

def pause_song(self):
    self.is_paused = True


root = Tk()
myMp3 = MP3(root)
root.mainloop()
bang bang
  • 19
  • 2
  • 1
    I can't provide a detailed answer but I would suggest using threads. You can create a new thread in which the audio is played. – scotty3785 Nov 08 '18 at 14:01
  • I agree with scotty. You will need to use threading to manage the audio as to not interrupt the mainloop in tkinter. – Mike - SMT Nov 08 '18 at 14:05
  • I imagine that your program *'freezes'* because you're not just trying to play the file once, but you're actually looping as fast as you possibly can and calling `play(song)` many times. – amitchone Nov 08 '18 at 14:11
  • 1
    @AdamMitchell that is another issue as well. The OP will need to correct both. – Mike - SMT Nov 08 '18 at 14:17
  • thanks guys for your comments. I'm not that familiar with threads and want to learn more about them and how to use them. Is there a good resource or way to learn more about threads? – bang bang Nov 11 '18 at 13:17

1 Answers1

0

I recommend using simpleaudio (e.g. pip install simpleaudio) like suggested in the pydub readme

If you use pydub.playback.play() it will still wait for the playback to finish, but pydub.playback._play_with_simpleaudio() will run in a thread and not block the interpreter.

You'll probably want to use the _play_with_simpleaudio() function as a starting point for your own playback functionality which uses simpleaudio directly

Jiaaro
  • 74,485
  • 42
  • 169
  • 190