赞
踩
官方示例代码中心:
https://console.bce.baidu.com/support/?_=1668482508529#/api?product=AI&project=语音技术&parent=语音合成&api=text2audio&method=post
一、调试时,若提示 16: Open api characters limit reached({"convert_offline":false,"err_detail":"16: Open api characters limit reached...),表明没有合成语音取资源,按照如下路径依次进入开通领取资源即可:
登录百度只智能云 - 控制台 - 语音技术 - 服务列表 - 语音合成,根据提示,开通领取资源。
二、如何查看已领取资源:
登录百度只智能云 -控制台 - 语音技术 - 服务列表 - 资源列表 - 语音合成。
- class Sample {
- const API_KEY = "oA4DWdlt3KIy...";
- const SECRET_KEY = "M5bBWfEGdFLh...";
-
- public function run() {
- $curl = curl_init();
- curl_setopt_array($curl, array(
- CURLOPT_URL => "https://tsn.baidu.com/text2audio", //http://tsn.baidu.com/text2audio || https://tsn.baidu.com/text2audio
- CURLOPT_TIMEOUT => 30,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_CUSTOMREQUEST => 'POST',
-
- CURLOPT_POSTFIELDS => http_build_query(array(
- // 合成的文本,使用UTF-8编码。不超过60个汉字或者字母数字。文本在百度服务器内转换为GBK后,长度必须小于120字节。如需合成更长文本,推荐使用长文本在线合成
- 'tex' => 'Hello 你好,短文本在线合成',
-
- // 开放平台获取到的开发者access_token(见上面的“鉴权认证机制”段落)
- 'tok' => ''. $this->getAccessToken() .'',
-
- // 用户唯一标识,用来计算UV值。建议填写能区分用户的机器 MAC 地址或 IMEI 码,长度为60字符以内
- 'cuid' => 'ajVXW9G6XEYG9sUFbntQJjhZPhMp8ioK',
-
- // 客户端类型选择,web端填写固定值1
- 'ctp' => '1',
-
- // 固定值zh。语言选择,目前只有中英文混合模式,填写固定值zh
- 'lan' => 'zh',
-
- // 语速,取值0-15,默认为5中语速
- 'spd' => '5',
-
- // 音调,取值0-15,默认为5中语调
- 'pit' => '5',
-
- // 音量,取值0-15,默认为5中音量(取值为0时为音量最小值,并非为无声)
- 'vol' => '5',
-
- // 度小宇=1,度小美=0,度逍遥(基础)=3,度丫丫=4 | 度逍遥(精品)=5003,度小鹿=5118,度博文=106,度小童=110,度小萌=111,度米朵=103,度小娇=5
- 'per' => '1',
-
- // 3为mp3格式(默认); 4为pcm-16k;5为pcm-8k;6为wav(内容同pcm-16k); 注意aue=4或者6是语音识别要求的格式,但是音频内容不是语音识别要求的自然人发音,所以识别效果会受影响。
- 'aue' => '3'
- )
- ),
-
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: application/x-www-form-urlencoded',
- 'Accept: */*'
- ),
-
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- return $response;
- }
-
- /**
- * 使用 AK,SK 生成鉴权签名(Access Token)
- * @return string 鉴权签名信息(Access Token)
- */
- private function getAccessToken(){
- $curl = curl_init();
- $postData = array(
- 'grant_type' => 'client_credentials',
- 'client_id' => self::API_KEY,
- 'client_secret' => self::SECRET_KEY
- );
- curl_setopt_array($curl, array(
- CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
- CURLOPT_CUSTOMREQUEST => 'POST',
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_POSTFIELDS => http_build_query($postData)
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- $rtn = json_decode($response);
- return $rtn->access_token;
- }
- }
-
- $rtn = (new Sample())->run();
- print_r($rtn);
- die;

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。