Use python to play sound files (mp3, wav, m4a, etc.)

Use python to play sound files (mp3, wav, m4a, etc.)

Some time ago, I was working on a python-based voice assistant, which requires the function of python to play audio. It needs to run on windows and Raspberry Pi. But after searching on the Internet for a long time, I did not find a suitable solution (pygame and PyAudio). Can barely be used, but the effect is not ideal). I had no choice but to Google and found a more basic article, which introduced in detail how to use some libraries to realize the function of playing audio, which is simple and practical. For the convenience of future use, I have a brief translation record of the article as follows:

Original address: https://pythonbasics.org/python-play-sound/

【The main idea of ​​the article】

Playing sound files with python is very simple. Here are some third-party libraries that can play sounds. These solutions are cross-platform and can be used on windows, Mac and Linux.

The main difference between these methods is the simplicity and supported file types. They all support the python3 environment. When programming, make sure that the .py file and the sound file are in the same directory, or specify the absolute address of the sound file in the code.

Method 1: playsound module

The playsound module is a cross-platform library that does not require other dependent libraries. It can be installed directly using pip or IDE library management functions.

from playsound import playsound
 
playsound(‘test.mp3’)

You only need the above two lines of code to hear the sound. Can be used to play mp3 and wav files, etc.

Method two: pydub

It can be installed via pip, and pydub can also call the underlying PyAudio and ffmpeg libraries.


from pydub import AudioSebment
 
from pydub.playback import play

song = AudioSegment.from_wav(‘test.wav’)
 
play(song)

Method 3: snack sound kit

This is more powerful and can play wav, AU, AIFF, MP3, CSL, SD, SMP and NIST/Sphere files, but this library has not been updated and maintained for a long time.

It can be installed via apt install python3-tksnack, the old version is named python-tksnack.

This library needs Tkinker support, which means that you must install Tkinter to use it.

from Tkinker import *
import tkSnack
root = Tk()
tkSnack.installzeSnack(root)
snd = tkSnack.Sound()
snd.read(‘test.mp3’)
snd.play(blocking = 1)

Method 4: Local player

You can use the computer or the Raspberry Pi's own player software to play audio files. You only need to specify the default player on windows. On Linux, you can install the playback software through the terminal, such as mpg123.

import os

file = ‘test.mp3’
 
os.system(‘mpg123’+ file)

[Time is in a hurry, the code has not had time to test one by one, if you are interested, you can read the original text https://pythonbasics.org/python-play-sound/]

3. Encountered a big hole in use: the file cannot be released from the occupation!

This module has a very big problem. If you want to replay, delete or move it, you will be prompted to deny access. And the big guys who made this module seem to have abandoned it.

I added the stop function to solve the occupancy problem based on the source code, but only changed the source code of this part of the windows system. I don't want to change mac and linux, I'm so tired! Friends in need can download and refer to my method to modify, the function is actually very simple.

The code address is as follows:
https://download.csdn.net/download/dorlolo/11155532

Reprinted in: here

Guess you like

Origin blog.csdn.net/lockhou/article/details/113781125