Qt(python) + 百度语音合成 实现demo


python实现

  • 安装api

    sudo pip3 install baidu-aip
  • 安装音频处理模块pydub

    sudo pip3 install pydub
from aip import AipSpeech
from pydub import AudioSegment
from pydub.playback import play
​
access_token = '24.4dff6ec411c7cd81fdc22c41b646358d.2592000.1590571668.282335-19******'
payload = {
           "cuid":"1",
           "ctp":"1",
           "aue":"3",
           "per":"5",
           "tok":access_token}
​
​
""" 你的 APPID AK SK """
APP_ID = '19631***'
API_KEY = 'vdIt2ch5uy749BrKDZNTG***'
SECRET_KEY = 'BLAzKrFGzzTXdd4lSeQFMb7L5IRFG***'
​
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
​
# 发送请求,下载语音音频
result = client.synthesis('今天是个好日子', 'zh', 2, payload)
​
# 识别正确返回语音二进制 错误则返回dict 返回正确保存在当前路径下
if not isinstance(result, dict):
    with open('audio.mp3', 'wb') as f:
        f.write(result)
​
song = AudioSegment.from_mp3("./audio.mp3")  # 播放音频
play(song)

 

Qt实现

  • 使用头文件#include

void Widget::on_pushButton_clicked()
{
    player=new QMediaPlayer(this);
    QString url="http://tsn.baidu.com/text2audio?";
    url.append(QString("lan=zh&cuid=1&ctp=1&tok=3&pit=8&per=3&tok=24.4dff6ec411c7cd81fdc22c41b646358d.2592000.1590571668.282335-19******"));   //将tok=后面换成自己的即可,token怎么获取可参考百度AI的文档
    url.append("&tex=");
    url.append(ui->textEdit->toPlainText());
​
    qDebug()<<url;
    player->setMedia(QUrl(url));
    player->play();
}

 

你可能感兴趣的:(Qt(python) + 百度语音合成 实现demo)