Play music with pygame

pygame is a python library for making 2D games but pygame have a class mixer.music which helps us to play music & perform some basic operations with music like stop the music playing , pause and unpause it ,set the volume, etc.



before moving ahead ,
pygame is not a built-in library so please make sure to install it before importing it .

>>> pip install pygame 

What will you learn from this blog .

The main focus of this blog is to introduced some of the basic functions of pygame for music/song manipulation .



1.pygame.mixer.music.load()

This function loads music file for playback .

import pygame       
pygame.init()  # initialize pygame
pygame.mixer.init()    # initialize mixer class
pygame.mixer.music.load('musicfile.mp3')  # load the music into playback
       


2.pygame.mixer.music.play( )

Plays the load music .

import pygame      
pygame.mixer.init()
pygame.mixer.music.load('musicfile.mp3')
pygame.mixer.music.play()


3.pygame.mixer.music.stop( )



import time
import pygame
pygame.mixer.init()
pygame.mixer.music.load('musicfile.mp3')
pygame.mixer.music.play()
time.sleep(10)    # after 10 seconds the music will stop playing
pygame.mixer.music.stop()
           


4.pygame.mixer.music.pause( ) 



Used for pause the playing song .
# tkinter : Dynamic Coding
# listbox ..

# import Library ...
from tkinter import *

def func():
	lbl['text'] = listbox.get(ACTIVE)
	

root = Tk()
root.title("Dynamic Coding")

lbl = Label(root,text='',font=('arial',12,'bold'),fg='cyan')
lbl.pack(pady=10)
# creating listbox
listbox = Listbox(root,font=('arial',12,'bold'),selectmode=SINGLE)
listbox.pack(pady=10)
# inserting data into it 
listbox.insert(END,'Python')
listbox.insert(END,'C++')
listbox.insert(END,'Java')
listbox.insert(END,'HTML')

# creating button for function call
Button(root,text='Get Data',command=func).pack(pady=10)

root.mainloop()

5.pygame.mixer.music.unpause( ) 

Used for unpause the paused song/music


import time
import pygame
pygame.mixer.init()
pygame.mixer.music.load('musicfile.mp3')
pygame.mixer.music.play()
time.sleep(10)    # after 10 seconds the music will pause
pygame.mixer.music.pause()
time.sleep(5)     # after 5 seconds the music will unpause
pygame.mixer.music.unpause()

S no. Option Discription
1 bd bd displays the border width
2 bg bg displays the background color of the canvas
3 confine This property doesn’t let you scroll outside the scroll region.
4 cursor You can use the cursor as different shapes on the screen as circle, dot etc.
5 height Displays the size of the canvas as in the Y dimension.
6 highlightcolor Displays the highlighted color in focus
7 relief This displays different types of borders like SUNKEN, RAISED, GROOVE, and RIDGE.
8 scrollregion Allows how much the area can be scrolledin TOP, RIGHT, LEFT and BOTTOM poistions.
9 width Sets the width of the canvas


 
S.no.  Options  Description
1  bg  For backgroung color you can pass color name , hex code as well as RGB color combination.
     
     
     
     
     
     
     
     
 
S.no.  Options  Description
1  bg  For backgroung color you can pass color name , hex code as well as RGB color combination.
     
     
     
     
     df
   
     
     
 
1  %a   Locale’s abbreviated weekday name.
1  %A  Locale’s full weekday name.
2  %b   Locale’s abbreviated month name.
3  %B  Locale’s full month name.
4  %c  Locale’s appropriate date and time representation.
5  %d  Day of the month as a decimal number [01,31].
6  %H  Hour (24-hour clock) as a decimal number [00,23].
7  %I  Hour (12-hour clock) as a decimal number [01,12].
 Ctrl