db257 db257 - 13 days ago
104 0

No description

Python

MP3 Player

from tkinter import *
import os
import subprocess

library = "/path/to/library"

root = Tk()

w,h = 600, 600
#root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w,h))

root.focus_set()
playing = False

#------------------------
libraryList = os.listdir(library)
libraryIndex = 0

player = None
playing = False

#------------------------    
def changeLibraryIndex(dire): #Increment/Decrement library Index
    global libraryIndex
    if dire == "Next":
        libraryIndex += 1
        if libraryIndex == len(libraryList):
            libraryIndex = 0
    if dire == "Back":
        libraryIndex -= 1
        if libraryIndex == -1:
            libraryIndex = len(libraryList)-1

def musicPlay():
    global player, playing
    if musicPlayButton["text"] == "Play": #If not paused
        musicPlayButton["text"] = "Pause"
        if playing == False: #If player isn't loaded
            songString = library + "/" + libraryList[libraryIndex]
            player = subprocess.Popen(["omxplayer","-o","local",songString],stdin=subprocess.PIPE)
            playing = True
        elif playing == True: #If player is loaded
            player.communicate(input=(bytes('q',"UTF-8")))
    elif musicPlayButton["text"] == "Pause":
        musicPlayButton["text"] = "Play"
        player.communicate(input=(bytes('q',"UTF-8")))
    
def musicStop(): #Stop music player
    global player, playing
    if player != None:
        player.communicate(input=bytes('q',"UTF-8"))
    musicPlayButton["text"] = "Play"
    player, playing = None, False

def musicBack(): #Play previous song
    changeLibraryIndex("Back")
    musicStop()
    musicPlay()
       
def musicNext(): #Play next song
    changeLibraryIndex("Next")
    musicStop()
    musicPlay()

def endProg(): #End GUI
    player = None
    root.destroy()
    #os.system("sudo shutdown now")

#------------------------

musicBackButton = Button(root,text="Back",width=5,command=musicBack)
musicPlayButton = Button(root,text="Play",width=5,command=musicPlay)
musicNextButton = Button(root,text="Next",width=5,command=musicNext)
musicStopButton = Button(root,text="Stop",width=5,command=musicStop)
quitButton = Button(root,text="End",command=endProg)

musicBackButton.pack(padx=10,side=LEFT)
musicPlayButton.pack(padx=10,side=LEFT)
musicNextButton.pack(padx=10,side=LEFT)
musicStopButton.pack(padx=10,side=LEFT)
quitButton.pack(padx=10,side=LEFT)

root.mainloop()

#-------------------------