https://pythonjishu.com/tzzjavbepoesojm/
安装
1
1 2 | pip install pyttsx3 pip install gtts |
2
报错的话
OSError: libespeak.so.1: cannot open shared object file: No such file or directory
1 2 | sudo apt - get update sudo apt - get install espeak |
3 编译
1 2 3 4 5 | $ git clone https: / / github.com / caixxiong / espeak - data $ cd espeak - data / $ unzip espeak - data. zip $ sudo cp - r * / usr / lib / arm - linux - gnueabihf / espeak - data / $ sudo espeak - - compile = zh |
综合实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import pyttsx3 import pyaudio import time #from gtts import gTTS from multiprocessing import Process,Manager class Voice(): #def __init__(self,ShareDatas,Lock): def __init__( self ): # 初始化 TTS 引擎 self .engine = pyttsx3.init() self .engine.setProperty( "rate" , 150 ) # 设置语速 self .engine.setProperty( "volume" , 0.8 ) # 设置音量 # 定义待朗读的中文文本 self .text = "世界上没有垃圾,只有被人滥用的资源。" # 设置 TTS 引擎的语言为中文 self .engine.setProperty( "voice" , "zh" ) # 进行语音合成 #engine.say(text) #engine.runAndWait() def Say( self ,text,voice_name = 'temp.mp3' ): self .engine.say(text) self .engine.runAndWait() with open (voice_name, "wb" ) as file : self .engine.save_to_file(text, voice_name) self .engine.runAndWait() def Play( self ,voice_name = "temp.mp3" ): #return # 播放合成的音频 chunk = 1024 p = pyaudio.PyAudio() stream = p. open ( format = p.get_format_from_width( 2 ), channels = 1 , rate = 44100 , output = True ) with open (voice_name, "rb" ) as file : data = file .read(chunk) while data: stream.write(data) data = file .read(chunk) stream.stop_stream() stream.close() p.terminate() def Vioce_PLAY(ShareImages,lock): Voice_ = Voice() while 1 : if ShareImages[ 9 ] = = 1 : print ( "vioce break" ) break if ShareImages[ 1 ] = = 1 : ShareImages[ 1 ] = 0 msg = ShareImages[ 2 ] Voice_.Say(msg) time.sleep( 4 ) else : time.sleep( 0.001 ) ''' if __name__ == '__main__': lock = Manager().Lock()#创建共享内存容器 ShareImages=Manager().dict()#存str类型数据 ShareImages[1]=0#用于控制保存图像和开始识别 ShareImages[2]="购买物品是"#计算结果 播报 ShareImages[9]=0 p = Process(target=Vioce_PLAY, args=(ShareImages,lock))#开启进程 p.deamon=True #伴随主进程关闭而关闭,但是有时候还是关闭不了,单独搞个标志位来控制 p.start()#开始 i=0 while 1: time.sleep(3) if ShareImages[1]==0: ShareImages[1]=1 i=i+1 ShareImages[2]="购买物品是"+str(i) else: pass ''' |
单纯播放mp3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import time import pygame import os from mutagen.mp3 import MP3 from multiprocessing import Process,Manager def GET_mp3List(): #获取地址并拼接 path_mp3 = 'mp3/' mp3_list_Path = [] for i in os.listdir(path_mp3): s = os.path.join(path_mp3,i) mp3_list_Path.append(s) print (i) return mp3_list_Path def Mp3_paly_all(): mp3_list_Path = GET_mp3List() if len (mp3_list_Path) = = 0 : print ( "没有数据" ) return 0 for Path_i in mp3_list_Path: # 获取每一首歌的时长 audio = MP3(Path_i) print ( "歌曲时间" , int (audio.info)) pygame.mixer.init() path = Path_i pygame.mixer.music.load(path) pygame.mixer.music.play( 10 ) #播放载入的音乐。该函数立即返回,音乐播放在后台进行 time.sleep( int ( 10 )) pygame.mixer.music.pause() # 暂停 time.sleep( int ( 3 )) pygame.mixer.music.unpause() # 接着播放 #获取歌曲时间 time.sleep( int (audio.info.length)) def Mp3_paly_while(ShareImages,lock): pygame.mixer.init() while 1 : if ShareImages[ 9 ] = = 1 : print ( "mp3 stop" ) if ShareImages[ 6 ] = = "paly" : ShareImages[ 6 ] = "wait" path = ShareImages[ 7 ] pygame.mixer.music.load(path) print ( "开始播放歌曲" ,path) #audio = MP3(path) #print("歌曲时间",int(audio.info)) pygame.mixer.music.play() elif ShareImages[ 6 ] = = "stop" : ShareImages[ 6 ] = "wait" pygame.mixer.music.pause() # 暂停 time.sleep( 0.0001 ) ''' if __name__ == '__main__': ShareImages=Manager().dict() lock=Manager().Lock() ShareImages[6]="wait"# ,mp3 动作 ShareImages[7]="wait"# ,mp3 当前播放名字 ShareImages[9]=0#用于关闭所有进程 #1采集图像进程 t1=Process(target=Mp3_paly_while,args=(managerdata,sharelock)) t1.start() t1.deamon=True #伴随主进程关闭而关闭 while 1: pass ''' |
单独播放mp3
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2022-12-26 python解析rosbag
2022-12-26 c/c++非阻塞键盘输入监听 Windows/Linux
2021-12-26 七彩爱心灯终版(二) 程序开发
2019-12-26 论文画图工具使用(3)英文矫正工具--谷歌翻译和1Checker
2018-12-26 微信硬件平台(三) 菜单开发
2018-12-26 微信硬件(三)
2017-12-26 wifi 安卓手机