mjbrusso / AudioPlayer

audioplayer is a cross platform Python 3 package for playing sounds (mp3, wav, ...). It provides the key features of an audio player, such as opening a media file, playing (loop/block), pausing, resuming, stopping, and setting the playback volume.
MIT License
37 stars 11 forks source link

Another stop and resume issue #13

Closed nLaykon closed 2 years ago

nLaykon commented 2 years ago

So i'm a student learning Python and i was trying to practice a bit and thought to make a GUI audio player, im having issues with the stop and pause buttons, they dont work at all when i use block=True on play and the music doesnt play when i use block=False

My code is the following: from tkinter import filedialog from tkinter import * import math import tkinter.messagebox import audioplayer from audioplayer import AudioPlayer

root = Tk() root.title("Mp3 Player") root.configure(background = 'grey') root.resizable(width=True, height=True) root.geometry("720x480") musicMp3 = filedialog.askopenfilename(parent=root, initialdir="/", title='Please Select a file') def pickm(): musicMp3 = filedialog.askopenfilename(parent=root, initialdir="/", title='Please Select a file')

def playm(): AudioPlayer(musicMp3).play(block=True)

def pausem(): AudioPlayer(musicMp3).stop()

def resumem(): AudioPlayer(musicMp3).resume()

pickbutton = Button(master=root, text="Pick Song", command=pickm, height=4, width=8, bg='aqua') pickbutton.pack()

playbutton = Button(master=root, text="Play Song", command=playm, height=4, width=8, bg='aqua') playbutton.pack()

pausebutton = Button(master=root, text="Pause", command=pausem, height=4, width=8, bg='aqua') pausebutton.pack()

resumebutton = Button(master=root, text="Resume", command=resumem, height=4, width=8, bg='aqua') resumebutton.pack()

root.mainloop()

nLaykon commented 2 years ago

im using Tkinter as GUI building purely because its built in with python 3 and I don't know any alternatives

reticulatus commented 2 years ago

The first thing I noticed was that you are calling AudioPlayer() in each of the playm(), pausem() and resumem() functions. That will create a separate AudioPlayer object in each function, instead of all of them working on the same object. You need to save a reference to a single global AudioPlayer object in the playm() function and have the pausem() and resumem() functions use that same reference, instead of creating their own AudioPlayer objects.

Secondly, setting block=True will block the rest of the program from working (including the HCI controls) until the music file has finished playing.

There is an example of a Tkinter music player included with the AudioPlayer source code, see: https://github.com/mjbrusso/AudioPlayer/blob/master/example/playerGUI.py

nLaykon commented 2 years ago

The first thing I noticed was that you are calling AudioPlayer() in each of the playm(), pausem() and resumem() functions. That will create a separate AudioPlayer object in each function, instead of all of them working on the same object. You need to save a reference to a single global AudioPlayer object in the playm() function and have the pausem() and resumem() functions use that same reference, instead of creating their own AudioPlayer objects.

Secondly, setting block=True will block the rest of the program from working (including the HCI controls) until the music file has finished playing.

There is an example of a Tkinter music player included with the AudioPlayer source code, see: https://github.com/mjbrusso/AudioPlayer/blob/master/example/playerGUI.py

thank you for the reply, unfortunately the music doesn't play in the 1st place if i have block false, i will take the other points and test them out, thanks for the help :)

nLaykon commented 2 years ago

@reticulatus editing my script to the following fixed my issue, thank you on the input of the global audio player 😄

rom tkinter import filedialog from tkinter import * import math import tkinter.messagebox import audioplayer from audioplayer import AudioPlayer

root = Tk() root.title("Mp3 Player") root.configure(background = 'grey') root.resizable(width=True, height=True) root.geometry("720x480") musicMp3 = filedialog.askopenfilename(parent=root, initialdir="/", title='Please Select a file') aud = audioplayer.AudioPlayer(musicMp3) def pickm(): musicMp3 = filedialog.askopenfilename(parent=root, initialdir="/", title='Please Select a file')

def playm(): aud.play()

def pausem(): aud.stop()

def resumem(): aud.resume()

pickbutton = Button(master=root, text="Pick Song", command=pickm, height=4, width=8, bg='aqua') pickbutton.pack()

playbutton = Button(master=root, text="Play Song", command=playm, height=4, width=8, bg='aqua') playbutton.pack()

pausebutton = Button(master=root, text="Pause", command=pausem, height=4, width=8, bg='aqua') pausebutton.pack()

resumebutton = Button(master=root, text="Resume", command=resumem, height=4, width=8, bg='aqua') resumebutton.pack()

root.mainloop()

reticulatus commented 2 years ago

Happy to help.

One other thing I noticed was that you call aud.stop() in the pausem() function. This causes the track to start from the beginning when you call resume().

To get it do proper pause/resume, your pausem() function should call aud.pause().

nLaykon commented 2 years ago

Happy to help.

One other thing I noticed was that you call aud.stop() in the pausem() function. This causes the track to start from the beginning when you call resume().

To get it do proper pause/resume, your pausem() function should call aud.pause().

I'm not having that issue, the pause and resume buttons work as intended. one of us may not have an up to date module

nLaykon commented 2 years ago

Happy to help. One other thing I noticed was that you call aud.stop() in the pausem() function. This causes the track to start from the beginning when you call resume(). To get it do proper pause/resume, your pausem() function should call aud.pause().

I'm not having that issue, the pause and resume buttons work as intended. one of us may not have an up to date module

that said, I updated it to use aud.pause() anyway

nLaykon commented 2 years ago

https://user-images.githubusercontent.com/88331977/147366473-0d391110-b9ab-4cc7-8a9d-7ba9cb4b58da.mp4