Volume MP3 and Python, PyGame - learnit

Home Top Ad

Post Top Ad

Friday, July 9, 2021

Volume MP3 and Python, PyGame

Volume MP3 and Python, PyGame

Volume MP3 and Python, PyGame

> What is Python?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python is simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.


Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program does not catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python's introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.


PyGame

pygame is a free and open-source cross-platform library for the development of multimedia applications like video games using Python. It uses the Simple DirectMedia Layer library and several other popular libraries to abstract the most common functions, making writing these programs a more intuitive task.


tkinter filedialog

Python Tkinter (and TK) offer a set of dialogs that you can use when working with files. By using these you do not have to design standard dialogs your self. File dialogs help you open, save files or directories. This is the type of dialog you get when you click file, open. This dialog comes out of the module; there is no need to write all the code manually.


Example


from tkinter import *
import pygame
from tkinter import filedialog
import time
from mutagen.mp3 import MP3
import tkinter.ttk as ttk
root= Tk()
root.title('reanits.com')
root.iconbitmap('1.png')
root.geometry('300x400')
pygame.mixer.init()
def playtime():
    if stopped:
        return
    Currenttime = pygame.mixer.music.get_pos()/1000
    ConvertCurrentTime = time.strftime('%M:%S',time.gmtime(Currenttime))
    song = songbox.get(ACTIVE)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    songmut = MP3(song)
    global SongLength
    SongLength=songmut.info.length
    ConvertSongLength = time.strftime('%M:%S', time.gmtime(SongLength))
    Currenttime += 1
    if int(myslider.get()) == int(SongLength):
        statusbar.config(text=f'Time Elasped:{ConvertSongLength} ')
    elif paused:
        pass
    elif int(myslider.get()) == int(Currenttime):
        SliderPosition = int(SongLength)
        myslider.config(to=SliderPosition, value=int(Currenttime))
    else:
        SliderPosition = int(SongLength)
        myslider.config(to=SliderPosition, value=int(myslider.get()))
        ConvertCurrentTime = time.strftime('%M:%S', time.gmtime(int(myslider.get())))
        statusbar.config(text=f'Time Elasped: {ConvertCurrentTime} of {ConvertSongLength} ')
        nexttime=int(myslider.get())+1
        myslider.config(value=nexttime)
    statusbar.after(1000,playtime)
def deletesongmusic():
    stopmusic()
    songbox.delete(ANCHOR)
    pygame.mixer.music.stop()
def deletesongsmusics():
    stopmusic()
    songbox.delete(0,END)
    pygame.mixer.music.stop()
def addsongmusic():
    song = filedialog.askopenfilename(initialdir='sound/', title="Choose File Song", filetypes=(("MP3 file", "*.mp3"),))
    song = song.replace("D:/Python/Test2/sound/","")
    song = song.replace(".mp3", "")
    songbox.insert(END,song)
def addsongsmusic():
    songs = filedialog.askopenfilenames(initialdir='sound/', title="Choose File Song", filetypes=(("MP3 file", "*.mp3"),))
    for song in songs:
        song = song.replace("D:/Python/Test2/sound/", "")
        song = song.replace(".mp3", "")
        songbox.insert(END, song)
def playmusic():
    global stopped
    stopped = False
    song = songbox.get(ACTIVE)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0)
   playtime()
global stopped
stopped = False
def stopmusic():
    statusbar.config(text='')
    myslider.config(value=0)
    pygame.mixer.music.stop()
    songbox.select_clear(ACTIVE)
    statusbar.config(text='')
    global stopped
    stopped = True
def nextmusic():
    if stopped:
        return
    statusbar.config(text='')
    myslider.config(value=0)
    nextone = songbox.curselection()
    nextone = nextone[0]+1
    song = songbox.get(nextone)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0)
    songbox.select_clear(0,END)
    songbox.activate(nextone)
    songbox.selection_set(nextone, last=None)
def preback():
    if stopped:
        return
    statusbar.config(text='')
    myslider.config(value=0)
    nextone = songbox.curselection()
    nextone = nextone[0] - 1
    song = songbox.get(nextone)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0)
    songbox.select_clear(0, END)
    songbox.activate(nextone)
    songbox.selection_set(nextone, last=None)
global paused
paused = False
def pause(is_paused):
    global paused
    paused = is_paused
    if paused:
        pygame.mixer.music.unpause()
        paused = False
    else:
        pygame.mixer.music.pause()
        paused = True
def slider(x):
    song = songbox.get(ACTIVE)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0, start=int(myslider.get()))
def volumn(x):
    pygame.mixer.music.set_volume(volumnslider.get())
masterframe = Frame(root)
masterframe.pack(pady=10)
songbox= Listbox(masterframe, bg="black", fg="green", width=80,selectbackground="gray",selectforeground="black")
songbox.grid(row=0, column=0)
backbtn= PhotoImage(file='image/forward101.png')
forwardbtn= PhotoImage(file='image/forward101.png')
playbtn= PhotoImage(file='image/Play101.png')
pausebtn= PhotoImage(file='image/pause101.png')
stopbtn= PhotoImage(file='image/Stop101.png')
controlframe= Frame(masterframe)
controlframe.grid(row=1,column=0,pady=20)

volumnframe = LabelFrame(masterframe,text="Volumn")
volumnframe.grid(row=0, column=1,padx=20)

backbutton= Button(controlframe, image=backbtn, borderwidth=0, command=preback)
frowardbutton= Button(controlframe, image=forwardbtn, borderwidth=0,command=nextmusic)
playbutton= Button(controlframe, image=playbtn, borderwidth=0, command=playmusic)
pausebutton= Button(controlframe, image=pausebtn, borderwidth=0, command=lambda: pause(paused))
stopbutton= Button(controlframe, image=stopbtn, borderwidth=0, command=stopmusic)

backbutton.grid(row=0, column=1, padx=10)
frowardbutton.grid(row=0, column=2, padx=20)
playbutton.grid(row=0, column=3, padx=20)
pausebutton.grid(row=0, column=4, padx=20)
stopbutton.grid(row=0, column=5, padx=20)

#create Menu
mymenu = Menu(root)
root.config(menu=mymenu)

addsong = Menu(mymenu)
mymenu.add_cascade(label="file", menu=addsong)
addsong.add_command(label="Open Song or Playlist",command=addsongmusic)
addsong.add_command(label="Open Many Songs or Playlist",command=addsongsmusic)

deletesong = Menu(mymenu)
mymenu.add_cascade(label="Delete", menu=deletesong)
deletesong.add_command(label="Delete Song or Playlist",command=deletesongmusic)
deletesong.add_command(label="Delete All Songs or Playlist",command=deletesongsmusics)
statusbar= Label(root, text='',bd=1, relief=GROOVE, anchor=E)
statusbar.pack(fill=X,side=BOTTOM, ipady=2)
myslider = ttk.Scale(masterframe,from_=0, to=100, orient=HORIZONTAL, value=0, command=slider, length=500)
myslider.grid(row=2,column=0,pady=20)

volumnslider = ttk.Scale(volumnframe,from_=0, to=1, orient=VERTICAL, value=1, command=volumn, length=160)
volumnslider.pack(pady=20)
root.mainloop()

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad