Python example method to open music files

In order of recommendation

1、 Use playsound library

fromplaysoundimportplaysound

playsound('xx.mp3')

2、 Use pygame library

frompygameimportmixer
importtime

mixer.init()
mixer.music.load('xx.mp3')
mixer.music.play()
time.sleep(5)
mixer.music.stop()

3、 Use mp3play library (only supports python2, not python3)

importmp3playimporttime

clip=mp3play.load('xx.mp3')
clip.play()
time.sleep(5)
clip.stop()

4、 Open the system's built-in player, and then play MP3, the pop-up window is troublesome and complicated

importos

os.system('xx.mp3')

Content expansion:

Examples of playing music files using pyaudio module

import pyaudio
import wave
import sys
chunk =1024
wf = wave.open('gyh.wav','rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
 channels=wf.getnchannels(),
 rate=wf.getframerate(),
 output=True)
data = wf.readframes(chunk)whilelen(data)0:
 stream.write(data)
 data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()

Recommended Posts

Python example method to open music files
Python3.7 debugging example method
3 ways to encrypt Python files
Python right alignment example method
What are the ways to open files in python
Method steps to increase python font
How to delete cache files in python
How to save text files in python
Python crawler example to get anime screenshots
Python Romberg method to find integral examples
Example method of reading sql from python
How to add background music in python
01. Introduction to Python
Python renames files
Python object-oriented example
How to delete files and directories in python
Implementation of Python headless crawler to download files
How to read and write files with Python
Introduction to Python
How does Python store data to json files
Example of how to automatically download pictures in python
Python implements FTP to upload files in a loop
Python magic method topic
Python is short-crawling music
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Python function-dictionary get() method
Python error handling method
Python interpolate interpolation example
Python play pause music
Python-openCV open operation example