Play sound in Python
文章推薦指數: 80 %
The playsound module is a cross platform module that can play audio files. This doesn't have any dependencies, simply install with pip in your virtualenv and ... PythonTutorial Home Exercises Course PlaysoundinPython PlaysoundonPythoniseasy.Thereareseveralmodulesthatcanplayasoundfile(.wav).Thesesolutionsarecrossplatform(Windows,Mac,Linux). Themaindifferenceisintheeaseofuseandsupportedfileformats.AllofthemshouldworkwithPython3.Theaudiofileshouldbeinthesamedirectoryasyourpythonprogram,unlessyouspecifyapath. Let’sexploretheoptions! Relatedcourse:CompletePythonProgrammingCourse&Exercises PlaysoundinPythonplaysoundmoduleTheplaysoundmoduleisacrossplatformmodulethatcanplayaudiofiles.Thisdoesn’thaveanydependencies,simplyinstallwithpipinyourvirtualenvandrun! 12fromplaysoundimportplaysoundplaysound('audio.mp3') Implementationisdifferentonplatforms.Ituseswindll.winmonWindows,AppKit.NSSoundonAppleOSXandGStreameronLinux. I’vetestedthiswithPython3.5.3.ThisshouldworkwithbothWAVandMP3files. pydubYoucanplaysoundfileswiththepydubmodule.It’savailableinthepypirepository(installwithpip).ThismodulecanusePyAudioandffmpegunderneath. 12345frompydubimportAudioSegmentfrompydub.playbackimportplaysong=AudioSegment.from_wav("sound.wav")play(song) snacksoundkitThemodulesnacksoundkitcanplayseveralaudiofiles:WAV,AU,AIFF,MP3,CSL,SD,SMP,andNIST/Sphere. Youcaninstallitwithyourpackagemanager:‘aptinstallpython3-tksnack’.Foroldversionsthere’s‘python-tksnack’. ThismoduledependsonTkinter.Thatmeansthattoplaysoundwiththismodule,you’dalsohavetoimporttheguimoduleTkinter.Themoduledoesn’tseemtohavebeenupdatedinawhile. 123456789fromTkinterimport*importtkSnackroot=Tk()tkSnack.initializeSnack(root)snd=tkSnack.Sound()snd.read('sound.wav')snd.play(blocking=1) nativeplayerYoucanalsoplaysoundsnativelyonyoursystem.Thisrequiresyoutohavesomekindofaudioplayerinstalledontheterminal.OnLinuxyoucanusempg123forthat. Thissimplyplaysthemp3filewithanexternalplayer. 123456#aptinstallmpg123importosfile="file.mp3"os.system("mpg123"+file) Relatedcourse:CompletePythonProgrammingCourse&Exercises Back CreateaPythonWebServer Next Texttospeechinpython
延伸文章資訊
- 1Python 播放音效簡易版. 概述 - Kiwi lee
以下找了幾種方法,按照需求選擇,不一定我最後選擇的就是最好的。 playsound; pyAudio; pyGame; cvlc. 方法. playsound. 安裝方法: pip instal...
- 2playsound - PyPI
Pure Python, cross platform, single function module with no dependencies for playing sounds.
- 3winsound —— Windows 系统的声音播放接口— Python 3.9.14 ...
duration 参数则指定了声音应持续的毫秒数。若系统无法让扬声器发声,则会触发 RuntimeError 。 winsound. PlaySound ( ...
- 4Play sound in Python
The playsound module is a cross platform module that can play audio files. This doesn't have any ...
- 5playsound的简介、安装、使用方法之详细攻略 - CSDN博客
playsound的简介. 纯Python,跨平台,单功能模块,不依赖播放声音。 播放声音模块只包含一个东西——函数(也 ...