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-playsound-git - AUR (en) - Arch Linux
Package Details: python-playsound-git 1.3.0-5 ... Description: Pure Python, cross platform, singl...
- 2Trying to play an MP3 through python with playsound. Not ...
#!/usr/bin/env python # import required module from playsound import playsound # for playing note...
- 3playsound的简介、安装、使用方法之详细攻略 - CSDN博客
playsound的简介. 纯Python,跨平台,单功能模块,不依赖播放声音。 播放声音模块只包含一个东西——函数(也 ...
- 4Python 播放音效簡易版. 概述 - Kiwi lee
以下找了幾種方法,按照需求選擇,不一定我最後選擇的就是最好的。 playsound; pyAudio; pyGame; cvlc. 方法. playsound. 安裝方法: pip instal...
- 5Python playsound - play mp3 sound files - CodersLegacy
The Python playsound library offers an easy and simple way to play sound files in Python. From al...