講故事機器人
阿新 • • 發佈:2018-11-26
#自己寫的介面 import os from aip import AipSpeech from playsound import playsound class Voice: def __init__(self): """ 你的 APPID AK SK,初始化 """ self.__APP_ID = '#' self.__API_KEY = '#' self.SECRET_KEY = '#' def token_acess(self): """身份驗證""" return AipSpeech(self.__APP_ID, self.__API_KEY, self.SECRET_KEY) def read_file(self, f): with open(f, "rb") as f: res = f.read() return res def voice(self, world): """語音播放""" client = self.token_acess() word = self.read_file(world) result = client.synthesis(word, 'zh', 1, { 'vol': 5, }) # 識別正確返回語音二進位制 錯誤則返回dict 參照下面錯誤碼 if not isinstance(result, dict): with open('a.mp3', 'wb') as f: f.write(result) playsound('a.mp3') return
#呼叫自己的介面,載入故事並且播放
from playsound import playsound
from voice_play import *
import os
import time
def main():
voice = Voice()
voice.voice("./story.txt")
main()