MP3 player with Python

2

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()
    
asked by anonymous 10.05.2017 / 16:38

1 answer

4
Funny, I just needed to move item += 1 and item -= 1 up, and it worked quietly. It was just that, you did everything right.

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()

To solve the breakthrough problem I've changed it:

The problem was that the variable item was going to -1, that it was impossible to find a song, so it would not come back. And the other problem is that it ( item ) was also increasing the value too much, going to a nonexistent song, so it did not progress.

I also took the for , I preferred to use a variable called item.

I seem to have solved most problems. The only problem I could not solve and that sometimes appears is that it does not read the song number 1 (That would be the second one), but it is not always.

I hope I have helped.

Here is the code I modified:

def proxima ():
    global item #Usando uma variável de fora
    item += 1
    #print(item) # Utilizei para checar o que havia de errado.
    try:
        musica_atual = mixer.music.load(musicas[item])
        musica_atual = mixer.music.play()
    except IndexError: # Se o Index nao existir, isso vai impedir que o valor de item incremente.
        item -= 1



def anterior ():
    global item
    if item - 1 == -1: # Se for -1 ele volta não decrementa, ficando em 0.
        pass
    else:
        item -= 1
    #print(item)
    musica_atual = mixer.music.load(musicas[item])
    musica_atual = mixer.music.play()

I also changed the escolher() function, I noticed that sometimes a '' was in the list, which caused the program not to go forward / backward, and I started to work on the desktop, which was better for me songs are on the desktop), so I did this:

def escolher ():
    selecionar = askopenfilename(initialdir="C:/Users/%user%/desktop",
                       filetypes =(("Arquivo de audio", "*.mp3"),("All Files","*.*")),
                       title = "Selecione as musicas"
                       )
    if selecionar == '': # Se tiver '' ele não faz nada ( pass )
        pass
    else:
        musicas.append(selecionar)
    
10.05.2017 / 17:31