开发者社区 问答 正文

我录制的音频在阿里语音AI上可以怎样用?

我录制的音频在阿里语音AI上可以怎样用?

展开
收起
三分钟热度的鱼 2023-05-29 14:42:39 138 分享 版权
1 条回答
写回答
取消 提交回答
  • 您可以使用阿里云语音识别服务来识别和转写您录制的音频文件。

    具体的操作步骤如下:

    1. 创建一个语音识别服务实例,并在控制台中记录下 API 密钥和密钥 ID。

    2. 将您录制的音频文件上传到阿里云 OSS 对象存储服务中,并记录下该音频文件的 OSS URL。

    3. 复制以下 Java 代码示例,并将其中的 "YOUR_APP_KEY"、"YOUR_APP_SECRET"、"YOUR_AUDIO_URL" 替换成您自己实际的信息:

    import java.util.List;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    import com.aliyuncs.voice20180510.model.CreateAsrVocabRequest;
    import com.aliyuncs.voice20180510.model.CreateAsrVocabResponse;
    import com.aliyuncs.voice20180510.model.SubmitAsrJobRequest;
    import com.aliyuncs.voice20180510.model.SubmitAsrJobResponse;
    import com.aliyuncs.voice20180510.model.SubmitAsrJobResponse.AsrResult;
    import com.aliyuncs.voice20180510.model.SubmitAsrJobResponse.AsrResult.SentenceResult;
    
    public class AliyunAsrExample {
        private static final String APP_KEY        = "YOUR_APP_KEY";       //您申请的AppKey
        private static final String APP_SECRET     = "YOUR_APP_SECRET";    //您申请的AppSecret
        private static final String REGION_ID      = "cn-shanghai";        //目前仅支持华东2(cn-shanghai)和华北2(cn-beijing)
        private static final String OSS_URL        = "YOUR_AUDIO_URL";     //音频文件的OSS URL
    
        public static void main(String[] args) {
            try {
                //初始化
                IClientProfile profile = DefaultProfile.getProfile(REGION_ID, APP_KEY, APP_SECRET);
                DefaultAcsClient client = new DefaultAcsClient(profile);
    
                //创建 ASR 词汇表(可选操作)
                CreateAsrVocabRequest createAsrVocabRequest = new CreateAsrVocabRequest();
                createAsrVocabRequest.setName("MyVocab1");           //词汇表名称
                createAsrVocabRequest.setVocabFileUrl(OSS_URL);      //词汇文件OSS URL
                CreateAsrVocabResponse createAsrVocabResponse = client.getAcsResponse(createAsrVocabRequest);
                System.out.println("Create Asr Vocab Result: " + createAsrVocabResponse);
    
                //提交 ASR 任务
                SubmitAsrJobRequest submitAsrJobRequest = new SubmitAsrJobRequest();
                submitAsrJobRequest.setAsrVocabId(createAsrVocabResponse.getAsrVocabId());         //关联的词汇表 ID(可选)
                submitAsrJobRequest.setFilePath(OSS_URL);
                submitAsrJobRequest.setScene("general");
                submitAsrJobRequest.setChannelId(1);
                SubmitAsrJobResponse submitAsrJobResponse = client.getAcsResponse(submitAsrJobRequest);
    
                //查询 ASR 任务结果
                AsrResult asrResult = null;
                while (asrResult == null) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    for (String jobId : submitAsrJobResponse.getData().getJobIds()) {
                        try {
                            asrResult = getAsrResult(client, jobId);
                            if (asrResult != null) {
                                break;
                            }
                        } catch (ServerException e) {
                            e.printStackTrace();
                            break;
                        } catch (ClientException e) {
                            e.printStackTrace();
                        }
                    }
                }
    
                //输出识别结果
                System.out.println("Asr Result:");
                for (SentenceResult sentence : asrResult.getSentenceResults()) {
                    System.out.println(sentence.getText());
                }
    
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    
        private static AsrResult getAsrResult(DefaultAcsClient client, String jobId)
                throws ServerException, ClientException {
            SubmitAsrJobResponse submitAsrJobResponse = new SubmitAsrJobResponse();
            submitAsrJobResponse.setJobIds(jobId);
            AsrResult asrResult = null;
            submitAsrJobResponse = client.getAcsResponse(submitAsrJobResponse);
            switch (submitAsrJobResponse.getData().getStatus()) {
            case "SUCCESS":
                asrResult = submitAsrJobResponse.getData().getAsrResult();
                break;
            case "FAILED":
                System.out.println("Failed to transcribe audio file, jobId: " + jobId);
                break;
            default:
                System.out.println("Job is not finished, jobId: " + jobId);
            }
            return asrResult;
        }
    }
    

    这段代码将会:

    • 将音频文件的 OSS URL 作为参数,提交阿里云语音识别服务的 ASR 任务;
    • 等待任务完成,返回识别结果。

    在程序运行完成后,您将会在控制台中看到通过语音识别转写之后的文本内容。

    2023-05-30 09:48:35
    赞同 展开评论
问答分类:
问答地址: