Play mp3 and stop mp3 in python - learnit

Home Top Ad

Post Top Ad

Friday, May 7, 2021

Play mp3 and stop mp3 in python

Play mp3 and stop mp3 in python

Play mp3 and stop mp3 in python

What is Python

    Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. Guido van Rossum during 1985- 1990, created it. 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 necessary 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 − the interpreter processes Python at runtime. You do not need to compile your program before executing it. This is similar to PERL and PHP.


   -Python is Interpreted − the interpreter processes Python at runtime. You do not need to compile your program before executing it. This is similar to PERL and PHP.


   -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 Pygame (Game Development Library)

    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.



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. Example dialogs include an open file dialog, a save file dialog and many others. Besides file dialogs there are other standard dialogs, but in this article we will focus on file dialogs.


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/ Code

from tkinter import *
import pygame
from tkinter import filedialog
root= Tk()
root.title('reanits.com')
root.iconbitmap('1.png')
root.geometry('300x400')
pygame.mixer.init()
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)
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)
frowardbutton= Button(controlframe, image=forwardbtn, borderwidth=0)
playbutton= Button(controlframe, image=playbtn, borderwidth=0, command=playmusic)
pausebutton= Button(controlframe, image=pausebtn, borderwidth=0)
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)
root.mainloop()

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad