banner



How To Make A Music Player In Python

In this tutorial, you're going to learn how to make your own MP3 Music Player in Python using pygame and Tkinter libraries which is able to load, play, pause, and stop the music.

Requirements

To exist able to follow this tutorial yous need to have Tkinter and pygame installed on your auto

Installation

  • Linux
                sudo apt-go install python3-tk  pip3 install pygame                              

  • Window
                pip install pygame                              

At present once everything is set Installed, we now set up to begin coding our very own music player

Allow's become started

We gonna use Tkinter to return our application menu and buttons, and also loading the music from files and then pygame library to play, suspension and terminate the music

Basics of pygame

Pygame has an inbuilt method chosen mixer () which provides us intuitive syntax on dealing with sounds files in python, we will see ;

  • loading and playing music
  • pausing and unpausing music
  • stoping the music file

loading and playing music with pygame

To play music with pygame yous're firstly supposed to import mixer(), initialize it through *init(), *and so using music.load() to load the music and finally playing it with music.play().

Example of usage

                from pygame import mixer  mixer.init() #Initialzing pyamge mixer  mixer.music.load.('filename.mp3') #Loading Music File  mixer.music.play() #Playing Music with Pygame                              

Pausing and unpausing music with pygame.

use* music.interruption()* and* music.unpause()* to pause and unpause your music, but make your music file is loaded first before using these methods.

Example of usage

                mixer.music.interruption() #pausing music file  mixer.music.unpause() #unpausing music file                              

Stop a music file

use music.stop() to stop your music completely from playing, in one case you use this method the loaded music volition be cleared in pygame retention.

                mixer.music.stop()                              

Building your music actor exoskeleton

We have already covered the nuts of playing music with python, now it'due south time to begin designing our awarding user interface{UI) and then link together with the logics.

First Let u.s.a. import all necessary Library

                from tkinter import *  from tkinter import filedialog  from pygame import mixer                              

Permit'due south now implement our course & Buttons for our application

                class MusicPlayer:     def __init__(self, window ):         window.geometry('320x100'); window.title('Iris Player'); window.resizable(0,0)         Load = Button(window, text = 'Load',  width = 10, font = ('Times', 10), command = self.load)         Play = Button(window, text = 'Play',  width = ten,font = ('Times', x), command = self.play)         Pause = Button(window,text = 'Intermission',  width = ten, font = ('Times', x), command = self.pause)         Stop = Button(window ,text = 'End',  width = 10, font = ('Times', 10), command = self.stop)         Load.place(x=0,y=20);Play.place(x=110,y=20);Pause.identify(10=220,y=xx);Stop.place(10=110,y=threescore)         self.music_file = False         cocky.playing_state = Fake                              

Let's now add the method to the course nosotros just fabricated to load music file from our computer, simply as shown in the code below

Adding Load Method to our MusicPlayer class

                class MusicPlayer:     def __init__(self, window ):         window.geometry('320x100'); window.title('Iris Player'); window.resizable(0,0)         Load = Button(window, text = 'Load',  width = ten, font = ('Times', 10), command = cocky.load)         Play = Push button(window, text = 'Play',  width = 10,font = ('Times', 10), command = self.play)         Suspension = Push button(window,text = 'Interruption',  width = 10, font = ('Times', ten), command = self.pause)         Finish = Button(window ,text = 'Terminate',  width = 10, font = ('Times', ten), control = cocky.stop)         Load.place(10=0,y=20);Play.identify(x=110,y=20);Pause.place(10=220,y=xx);Stop.place(x=110,y=60)         self.music_file = False         self.playing_state = False     def load(cocky):         self.music_file = filedialog.askopenfilename()                              

Later on Loading the Music file from the file we demand a function to Play our Music File, Allow'south make it using the concepts we just learned above.

Adding Play Method to our class

                course MusicPlayer:     def __init__(self, window ):         window.geometry('320x100'); window.title('Iris Actor'); window.resizable(0,0)         Load = Push button(window, text = 'Load',  width = 10, font = ('Times', 10), command = cocky.load)         Play = Push(window, text = 'Play',  width = 10,font = ('Times', 10), control = self.play)         Pause = Button(window,text = 'Intermission',  width = 10, font = ('Times', 10), command = self.suspension)         Finish = Button(window ,text = 'Stop',  width = x, font = ('Times', 10), control = self.cease)         Load.place(10=0,y=20);Play.identify(x=110,y=twenty);Pause.place(x=220,y=twenty);Finish.place(10=110,y=60)         self.music_file = Fake         cocky.playing_state = Faux     def load(cocky):         self.music_file = filedialog.askopenfilename() 	def play(self):         if self.music_file:             mixer.init()             mixer.music.load(self.music_file)             mixer.music.play()                              

After adding the Play Method to our grade nosotros need a Method to suspension and unpause & also to Stop the Music

Finally Allow'south add the pause and end method to our course

                class MusicPlayer:     def __init__(self, window ):         window.geometry('320x100'); window.championship('Iris Thespian'); window.resizable(0,0)         Load = Button(window, text = 'Load',  width = x, font = ('Times', x), command = self.load)         Play = Button(window, text = 'Play',  width = 10,font = ('Times', 10), control = cocky.play)         Pause = Button(window,text = 'Pause',  width = 10, font = ('Times', 10), control = self.intermission)         Stop = Button(window ,text = 'Stop',  width = 10, font = ('Times', 10), command = self.stop)         Load.place(x=0,y=twenty);Play.place(x=110,y=20);Intermission.place(x=220,y=20);Stop.place(x=110,y=60)         self.music_file = False         self.playing_state = Imitation     def load(cocky):         self.music_file = filedialog.askopenfilename() 	def play(cocky):         if self.music_file:             mixer.init()             mixer.music.load(self.music_file)             mixer.music.play()     def pause(self):         if not cocky.playing_state:             mixer.music.pause()             cocky.playing_state=True         else:             mixer.music.unpause()             self.playing_state = False     def stop(self):         mixer.music.end()                              

Let's await at our Last will app (app.py)

                from tkinter import * from tkinter import filedialog from pygame import mixer  class MusicPlayer:     def __init__(self, window ):         window.geometry('320x100'); window.title('Iris Player'); window.resizable(0,0)         Load = Button(window, text = 'Load',  width = 10, font = ('Times', ten), command = self.load)         Play = Button(window, text = 'Play',  width = 10,font = ('Times', 10), control = self.play)         Pause = Button(window,text = 'Interruption',  width = 10, font = ('Times', 10), control = cocky.intermission)         Stop = Push button(window ,text = 'Stop',  width = x, font = ('Times', ten), command = cocky.stop)         Load.place(x=0,y=20);Play.place(x=110,y=xx);Pause.place(ten=220,y=twenty);Stop.place(x=110,y=threescore)          self.music_file = False         self.playing_state = False     def load(cocky):         self.music_file = filedialog.askopenfilename()     def play(self):         if self.music_file:             mixer.init()             mixer.music.load(self.music_file)             mixer.music.play()     def pause(self):         if not self.playing_state:             mixer.music.pause()             self.playing_state=Truthful         else:             mixer.music.unpause()             cocky.playing_state = False     def terminate(cocky):         mixer.music.stop() root = Tk() app= MusicPlayer(root) root.mainloop()                              

Output

If you run the above code the output volition be as shown beneath, now y'all can play around with it by loading, playing, pausing, and stopping the music as shown in the moving picture below;

In example of any comment,suggestion or difficulties drop it on the annotate box beneath, and then I will get dorsum to you ASAP.

To get the full code for this article, here a link to My GitHub

How To Make A Music Player In Python,

Source: https://kalebujordan.dev/make-your-own-music-player-in-python/

Posted by: elwellidents.blogspot.com

Related Posts

0 Response to "How To Make A Music Player In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel