How to play audio using python

Release: 2019-07-08 09:01:17
Original
11118 people have browsed it

How to play audio using python

There are several ways to use python to play audio:

os.system()

os.system(file ) Call the system application to open the file. file can be an image or audio file.

Disadvantages: To open a specific application, audio cannot be played in the background.

pyaudio

Installation: pip install pyaudio

The official API for playing audio and recording is very convenient to use. Just put Filename Change the text to your audio file and you can play the audio.

"""PyAudio Example: Play a WAVE file.""" import pyaudio import wave CHUNK = 1024 FILENAME = '你的音频文件' def play(filename = FILENAME): wf = wave.open(filename, '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) while data != b'': stream.write(data) data = wf.readframes(CHUNK) stream.stop_stream() stream.close() p.terminate()
Copy after login

jupyter notebook

You can use the following functions to play audio in jupyer notebook:

import IPython.display as ipd ipd.Audio(文件名)
Copy after login

For more Python related technical articles, please visitPython tutorialcolumn for learning!

The above is the detailed content of How to play audio using python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!