thinkphp5 接入百度語音合成介面,前臺播報功能例項
阿新 • • 發佈:2018-11-14
版本:thinkphp5
功能:語音播報功能
描述:前臺實現點選播報網頁內容的功能例項。
範圍:php原生及其他框架均可使用
1.下載百度php-sdk 版本:1.6.0 點此下載:http://www.chtml.cn/product/show/10888
2.引入例項:
public function baidu_spack(){ vendor('baidu_spack.AipSpeech'); $spak = input('get.spak'); $APP_ID = ' 通過百度開發者平臺建立應用後獲取'; $API_KEY = '通過百度開發者平臺建立應用後獲取'; $SECRET_KEY = '通過百度開發者平臺建立應用後獲取'; $client = new \AipSpeech($APP_ID,$API_KEY,$SECRET_KEY); $result = $client->synthesis($spak, 'zh', 1, array( 'vol' => 5, )); return $result;
注:下載後的sdk 必須要放在vendor目錄中才能使用以上程式碼。
3.前臺呼叫:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>百度文字轉語音介面demo -</title> <body> <b>該例項是把如下文字轉為語音(JavaScript實現):</b><br> <font color=red>測試百度文字轉語音介面 </font> <script type="text/javascript"> var zhText = "測試百度文字轉語音介面"; zhText = encodeURI(zhText); document.write("<audio autoplay=\"autoplay\">"); document.write("<source src=\"你的域名和方法路徑帶上get引數="+ zhText +"\" type=\"audio/mpeg\">"); document.write("<embed height=\"0\" width=\"0\" src=\"你的域名和方法路徑帶上get引數="+ zhText +"\">"); document.write("</audio>"); </script> </body> </html>
4.更換語速和聲音
$result = $client->synthesis($spak, 'zh', 1, array(
'vol' => 5,//播報速度
'dev_pid' => 1536,//播報聲音
));
5.dev_pid相關聲音詳情