pyttsx3 快速上手之:语音合成播报

pyttsx3 快速上手之:语音合成播报Pythonpyttsx3使用之:语音播报pyttsx3是python中最常用的文字转语音库,使用方便,功能较为完整首先安装pyttsx3lib:pipinstallpyttsx3然后封装下API,实现为speaker.py:importpyttsx3global__speak_engine__speak_engine=Nonedefsay(content): global__speak_engine ifnot__speak_engine:

大家好,又见面了,我是你们的朋友全栈君。

Python pyttsx3 快速上手之:语音合成播报

pyttsx3 是python中最常用的文字转语音库,使用方便,功能较为完整

安装 pyttsx3:

首先安装 pyttsx3 lib:

  • pip install pyttsx3

API封装

然后封装下pyttsx3 API,新建一个speaker.py 如下:
在这里插入图片描述

import pyttsx3

global __speak_engine
__speak_engine = None

def say(content):
	global __speak_engine
	if not __speak_engine:
		__speak_engine = pyttsx3.init()
		voices = __speak_engine.getProperty('voices')
		__speak_engine.setProperty('voice', voices[1].id)
		__speak_engine.setProperty('rate', 150)
		__speak_engine.setProperty('volume', 1)

	__speak_engine.say(content)
	__speak_engine.runAndWait()

其中Property的rate/voice/volume可以根据需要自行调整:

API使用

这样在使用中直接调用 say(…),就可以实现中英文语音播报了:

(python3可以直接调用播报中文)

使用举例:

在这里插入图片描述

from speaker import *


if __name__ == '__main__':
    say("Hello Howie Xue, we can speak by Python now")
	say("你好")


博主热门文章推荐:

一篇读懂系列:

LoRa Mesh系列:

网络安全系列:

嵌入式开发系列:

AI / 机器学习系列:


在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/153139.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号