tp中如何调用科大讯飞的语音转写

1.首先注册科大讯飞(https://www.xfyun.cn/),拿到语音转写的APPID以及SecretKey
2.将我的压缩包放到tp的扩展类库目录(extend)中。解压
在这里插入图片描述
3.在控制里面的调用方式
在这里插入图片描述
重点注意:
1.一定要是科大讯飞语音转写的APPID以及SecretKey
2.你的音频文件一定要是绝对路径,这里科大讯飞那边需要将你的文件上传到他们的服务端
3.建议调用的方式采用消息队列的方式,方式因回调慢,影响项目的浏览速度
使用非常简单如有问题联系qq:1219481875

代码

class Kdxf{
	private $lfasr_host = 'http://raasr.xfyun.cn/api';
    # 请求的接口名
    private $api_prepare = '/prepare';
    private $api_upload = '/upload';
    private $api_merge = '/merge';
    private $api_get_progress = '/getProgress';
    private $api_get_result = '/getResult';
    # 文件分片大小10M
    private $file_piece_sice = 10485760;
    # 转写类型
    private $lfasr_type = 0;
    # 是否开启分词
    private $has_participle = 'false';
    private $has_seperate = 'true';
    # 多候选词个数
    private $max_alternatives = 0;
    # 子用户标识
    private $suid = '';
    private $appid;
    private $secret_key;
    private $upload_file_path;
    private $obj;
    private $data;
    private $sig;
    public function __construct($appid,$secret_key) {
        $this->appid = $appid;
        $this->secret_key = $secret_key;
        $this->sig = new Sliceidgenerator();
    }
 
 	#主要运行函数
    public function run($file_path) {
    	$this->upload_file_path = $file_path;
        if($this->upload_file_path!=''){
        	#预处理
            $pre_result = json_decode($this->prepare_request(),true);
			//var_dump($pre_result);die();
            $taskid = $pre_result['data'];
            #上传文件
            $this->upload_request($taskid,$this->upload_file_path);
            $this->merge_request($taskid);

            while (true) {
                $progress = $this->get_progress_request($taskid);
                $progress_dic = json_decode($progress);
                if ($progress_dic->err_no != 0 && $progress_dic->err_no != 26605){
                    return;
                }else{
                    $data = $progress_dic->data;
                    $task_status = json_decode($data);
                    if ($task_status->status == 9){
                        //echo('The task ' . $taskid . ' is in processing, task status: ' . $task_status->desc);
                        //echo "<br />";
                        break;
                    }
                    //echo('The task ' . $taskid . ' is in processing, task status: ' . $task_status->desc);
                    //echo "<br />";
                    //sleep(0.1);
                }
                    
            }

            $data_all=$this->get_result_request($taskid=$taskid);
            $data_all = json_decode($data_all)->data;
            $data_all=json_decode($data_all);
            $message='';
            for ($i=0;$i<count($data_all);$i++){
                $message.=$data_all[$i]->onebest;
            }   
			
            $this->data=$message;
            return $this->data;
        }
    }
 
    public function get_data() {
        return $this->data;
    }

    public function gene_params($apiname,$taskid="",$slice_id=""){
        $appid = $this->appid;
        $secret_key = $this->secret_key;
        $upload_file_path = $this->upload_file_path;
        $ts=time();
        $md5 = md5($appid.$ts);
        $signa = hash_hmac('sha1',$md5,$secret_key,true);
        $signa=base64_encode($signa);
        $file_len = filesize($this->upload_file_path);
        $file_name = basename($this->upload_file_path);
        if($apiname== $this->api_prepare){
            $slice_num = 1;
            $param_dict['app_id'] = $appid;
            $param_dict['signa'] = $signa;
            $param_dict['ts'] = $ts;
            $param_dict['file_len'] = (string)($file_len);
            $param_dict['file_name'] = $file_name;
            $param_dict['slice_num'] = (string)($slice_num);
        }elseif($apiname== $this->api_upload){
            $param_dict['app_id'] = $appid;
            $param_dict['signa'] = $signa;
            $param_dict['ts'] = $ts;
            $param_dict['task_id'] = $taskid;
            $param_dict['slice_id'] = $slice_id;
        }elseif($apiname == $this->api_merge){
            $param_dict['app_id'] = $appid;
            $param_dict['signa'] = $signa;
            $param_dict['ts'] = $ts;
            $param_dict['task_id'] = $taskid;
            $param_dict['file_name'] = $file_name;
        }elseif($apiname == $this->api_get_progress || $apiname == $this->api_get_result){
            $param_dict['app_id'] = $appid;
            $param_dict['signa'] = $signa;
            $param_dict['ts'] = $ts;
            $param_dict['task_id'] = $taskid;
        }
        return $param_dict;
    }

    public function gene_request($apiname, $data, $files='', $headers=''){
        if($files!=''){
            $data['content'] =$files['content'];
        }
        $jsonStr =$data;
        if($apiname==$this->api_prepare||$apiname==$this->api_merge||$apiname==$this->api_get_progress||$apiname==$this->api_get_result){
            $header= array(
                "Content-Type"=>"application/x-www-form-urlencoded;charset=UTF-8",
            );
        }
        if($apiname==$this->api_upload){
            $header = array(
                "Content-Type"=>"multipart/form-data;",
            );
        }
        $url = $this->lfasr_host . $apiname;
        $rt=$this->http_post_json($url, $jsonStr, $header);
        $result = json_decode($rt[1],true);
        if ($result["ok"] == 0){
//          echo("success:".$apiname);
//          echo "<br />";
            return $rt[1];
        }else{
            return $rt[1];
        } 
    }
    # 预处理gene_request
    public function prepare_request(){
         return $this->gene_request($apiname=$this->api_prepare,
                                 $data=$this->gene_params($this->api_prepare));
    }
    #上传
    public function upload_request($taskid, $upload_file_path){
        $finfo    = finfo_open(FILEINFO_MIME_TYPE);
        $mime = finfo_file($finfo, $upload_file_path);
        finfo_close($finfo);
        $curlFile = curl_file_create(
            $upload_file_path,
            $mime,
            pathinfo(
                $upload_file_path,
                PATHINFO_BASENAME
            )
        );
        $files["filename"] = $this->gene_params($this->api_upload)["slice_id"];
        $files["content"] = $curlFile;
        $response = $this->gene_request($this->api_upload,
                                 $data=$this->gene_params($this->api_upload, $taskid=$taskid,$slice_id=$this->sig->getNextSliceId()),$files=$files);
        return $response;
    }

    public  function merge_request($taskid){
        return $this->gene_request($this->api_merge, $data=$this->gene_params($this->api_merge, $taskid=$taskid));
    }

    # 获取进度
    public  function get_progress_request($taskid){
        return $this->gene_request($this->api_get_progress, $data=$this->gene_params($this->api_get_progress, $taskid=$taskid));
    }
    
    # 获取结果
    public  function get_result_request($taskid){
        $result=$this->gene_request($this->api_get_result, $data=$this->gene_params($this->api_get_result, $taskid=$taskid));
        return $result;
    }
    
    #请求
    public  function http_post_json($url, $jsonStr,$header)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return array($httpCode, $response);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java调用科大讯飞语音转写功能需要进行以下几个步骤: 1. 首先,需要获取科大讯飞语音转写API的接入凭证。可以在科大讯飞的官方网站上注册并创建应用,获得AppID、API Key和API Secret。 2. 在Java引入科大讯飞提供的SDK包。可以通过Maven或手动引入jar包的方式将SDK包添加到Java项目。 3. 进行SDK的初始化配置。在Java代码,使用获得的AppID、API Key和API Secret进行初始化配置,以便调用接口。 4. 提供需要转写语音文件。将需要转写语音文件存储到本地或云端,并获取其本地路径或URL地址。 5. 通过调用SDK提供的相关接口,传入语音文件路径或URL地址,以及其他必要的参数(如语音格式、语言等),进行语音转写操作。 6. 处理结果。语音转写接口通常会返回转写结果的回调函数或回调地址,我们需要在回调函数转写结果进行处理或在回调地址获取转写结果。 7. 可选的后续处理。根据需要,可以对转写结果进行进一步处理,如文本分析、语义理解等。 需要注意的是,前述的步骤是一个简单的概述,具体的实现细节还需根据具体的开发环境和项目需要进行调整。可以参考科大讯飞提供的官方文档和示例代码,以及进行必要的调试和优化工作。通过以上步骤,我们可以在Java成功调用科大讯飞语音转写功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值