I do not know almost anything about programming and so I've been studying new possibilities, like POO and the like. Recently it gave me a willingness to try to learn Graphical Interface and also to create an MP3 player, all this in Python (all yesterday). I started to look for ways to satisfy my desire and I decided to put the two together in one project, but I'm having a serious problem, I can not go forward or rewind a song in my player. p>
I do not know if I did something wrong, but I tried everything and I could not solve this small and annoying problem. I'll be leaving my source below for you to review.
from pygame import mixer # Load the required library
from tkinter.filedialog import askopenfilename
from tkinter import *
musicas = []
class Reprodutor :
def __init__ (self):
pass
def escolher ():
selecionar = askopenfilename(initialdir="C:/Users/",
filetypes =(("Arquivo de audio", "*.mp3"),("All Files","*.*")),
title = "Selecione as musicas"
)
musicas.append(selecionar)
return musicas
def reproduzir ():
"""
mixer.init()
mixer.music.load('C:/Users/Andreza/Music/Jeff The Killer Theme Song Piano Version Sweet Dreams Are Made Of Screams.mp3')
mixer.music.play()
"""
mixer.init()
for item in musicas:
musica_atual = mixer.music.load(item)
musica_atual = mixer.music.play()
def parar ():
musica_atual = mixer.music.stop()
def pausar ():
musica_atual = mixer.music.pause()
def retomar ():
musica_atual = mixer.music.unpause() #Continua da local pausado
def proxima ():
for item in range(len(musicas)):
musica_atual = mixer.music.load(musicas[item])
musica_atual = mixer.music.play()
item += 1
def anterior ():
for item in range(len(musicas)):
musica_atual = mixer.music.load(musicas[item])
musica_atual = mixer.music.play()
item -= 1
player = Reprodutor
janela =Tk()
janela.title("REPRODUTOR - FÉLIX LICHT") #Titulo
#Esta parte é que está com problemas
bt_escolher = Button(janela, width=20, text="ADICIONAR MUSICAS", command=player.escolher)
bt_proxima = Button(janela, width=10, text="PROXIMA", command=player.proxima)
bt_anterior = Button(janela, width=10, text="ANTERIOR", command=player.anterior)
bt_escolher.place (x=10, y=50 )
bt_proxima.place (x=170, y=50)
bt_anterior.place (x=270, y=50)
bt_play = Button(janela, width=10, text="PLAY", command=player.reproduzir)
bt_pause = Button(janela, width=10, text="PAUSAR", command=player.pausar)
bt_stop = Button(janela, width=10, text="PARAR", command=player.parar)
bt_return = Button(janela, width=10, text="RETOMAR", command=player.retomar)
bt_play.place (x=10, y=0)
bt_pause.place (x=110, y=0)
bt_stop.place (x=210, y=0)
bt_return.place (x=310, y=0)
janela.geometry("1280x720+450+350")
janela.mainloop()
If anyone can help me, I'll be grateful.
Thanks
Edited:
After the modification, which was carried out based on the first response of the post, the program began to pass the song and return to music, however it does this procedure only once.
I tried to create a control system, where the for item would be matched to the last element of the list, and if you true it would go back to the value 0 of lita, but not even this worked.
===================================================== =================================
from pygame import mixer # Load the required library
from tkinter.filedialog import askopenfilename
from tkinter import *
musicas = []
TAM = len(musicas)
class Reprodutor :
def __init__ (self):
pass
def escolher ():
selecionar = askopenfilename(initialdir="C:/Users/",
filetypes =(("Arquivo de audio", "*.mp3"),("All Files","*.*")),
title = "Selecione as musicas",
#multiple = True
)
musicas.append(selecionar)
for i in musicas:
print(i, end=" ")
print()
print()
return musicas
def reproduzir ():
mixer.init()
for item in musicas:
musica_atual = mixer.music.load(item)
musica_atual = mixer.music.play()
def parar ():
musica_atual = mixer.music.stop()
def pausar ():
musica_atual = mixer.music.pause()
def retomar ():
musica_atual = mixer.music.unpause() #Continua da local pausado
#Próximo e Anterior com Modificações realizadas com base na resposta do Antony Gabriel
def proxima ():
for item in range(len(musicas)):
item += 1
musica_atual = mixer.music.load(musicas[item])
musica_atual = mixer.music.play()
def anterior ():
for item in range(len(musicas)):
item -= 1
musica_atual = mixer.music.load(musicas[item])
musica_atual = mixer.music.play()
player = Reprodutor
janela =Tk()
janela.title("REPRODUTOR - FÉLIX LICHT") #Titulo
#Esta parte é que está com problemas
bt_escolher = Button(janela, width=20, text="ADICIONAR MUSICAS", command=player.escolher)
bt_proxima = Button(janela, width=10, text="PROXIMA", command=player.proxima)
bt_anterior = Button(janela, width=10, text="ANTERIOR", command=player.anterior)
bt_escolher.place (x=10, y=50 )
bt_proxima.place (x=170, y=50)
bt_anterior.place (x=270, y=50)
bt_play = Button(janela, width=10, text="PLAY", command=player.reproduzir)
bt_pause = Button(janela, width=10, text="PAUSAR", command=player.pausar)
bt_stop = Button(janela, width=10, text="PARAR", command=player.parar)
bt_return = Button(janela, width=10, text="RETOMAR", command=player.retomar)
bt_play.place (x=10, y=0)
bt_pause.place (x=110, y=0)
bt_stop.place (x=210, y=0)
bt_return.place (x=310, y=0)
janela.geometry("410x80+450+350")
janela.mainloop()