How to build an MP3 Music Player with Python
Whatis Python
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). This tutorial gives enough understanding on Python programming language.
Why to Learn Python?
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.
Python is a MUST for students and working professionals to become a great Software Engineer specially when they are working in Web Development Domain. I will list down some of the key advantages of learning Python
    Python is Interpreted − Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP.
    Python is Interactive − You can actually sit at a Python prompt and interact with the interpreter directly to write your programs.
    Python is Object-Oriented − Python supports Object-Oriented style or technique of programming that encapsulates code within objects.
    Python is a Beginner's Language − Python is a great language for the beginner-level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games.
Python is the most popular programming language or nothing wrong to say that it is the next-generation programming language. In every emerging field in computer science, Python makes its presence actively. Python has vast libraries for various fields such as Machine Learning (Numpy, Pandas, Matplotlib), Artificial intelligence (Pytorch, TensorFlow), and Game development (Pygame,Pyglet).
PyGame
Pygame is a cross-platform set of Python modules, which is used to create video games.
It consists of computer graphics and sound libraries designed to be used with the Python programming language.
-Pygame was officially written by Pete Shinners to replace PySDL.
-Pygame is suitable to create client-side applications that can be potentially wrapped in a standalone executable
Introduction
In this article, I will show you how you can create your own music player using python
OK, I know I am a data analyst, but sometimes when I work with data, I like listening to music. Data Analysis with music can be a very interesting mix of inspiration and concentration for a data analyst or a data scientist. Building your own music player is an amazing way to learn more about python and its potential. Maybe some modules that I will use in this article could be helpful in the deployment of your next machine-learning model.
I import the following modules
from tkinter import *
import pygame
from tkinter import filedialog
So to create the interface, we will write the following code that permits to create the interface, add the title of the interface and set the dimension of the interface.
root= Tk()
root.title('reanits.blogspot.com')
root.iconbitmap('1.png')
root.geometry('300x400')
pygame.mixer.init()
Instead, the following code presents a variable called play_list which brings the interface to display the items to the user. In addition, and Create Function for addsongmusic
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)
#Insert or 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)
Example
from tkinter import *
import pygame
from tkinter import filedialog
root= Tk()
root.title('reanits.blogspot.com')
root.iconbitmap('1.png')
root.geometry('300x400')
pygame.mixer.init()
def deletesongmusic():
    songbox.delete(ANCHOR)
    pygame.mixer.music.stop()
def deletesongsmusics():
    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():
    song = songbox.get(ACTIVE)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0)
def stopmusic():
    pygame.mixer.music.stop()
   songbox.select_clear(ACTIVE)
def nextmusic():
    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():
    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
songbox= Listbox(root, bg="black", fg="green", width=80,selectbackground="gray",selectforeground="black")
songbox.pack(pady=20)
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(root)
controlframe.pack()
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)
root.mainloop()
Please Watching My Video is Below
No comments:
Post a Comment