how to play audio file in python code example

Example 1: play music from python

from playsound import playsound
playsound('audio.mp3')

Example 2: how to play sound in python

#TO playsound in your python code you need to install play sound module.
# To install playsound moudule-
#(1). Open Terminal 
#(2). "In terminal only" type "pip install playsound"
# After installing playsound you need to write-
import playsound from playsound
# syntax of playsound command
playsound("C:\\file_link\\file_name")
# note: ````in python you need to specify path using double back slashes "\\",
# instead of single forward or back slash````
# please reaad the starting comments also they are very useful(ignore if readed)
# Enjoy :)

Example 3: play .ogg files with python

# Install pyglet before starting from the command line
>>> -m pip install pyglet

# The code :
import pyglet				# Imports the module
pyglet.options['search_local_libs'] = True	# Allows pyglet to access files in your directory

my_music = pyglet.media.load("Name_Of_My_Song.ogg")	# Loads the audio file you want to play (it does not have to be a .ogg

my_player = pyglet.media.Player()	# Creates a player

my_player.queue(my_music)	# Adds your song to the players queue
my_player.loop = True 		# If you want the player to loop your song. By default this value is False
my_player.play()			# Starts playing your audio file when you run the app

pyglet.app.run()			# Runs the app

Tags:

Misc Example