目录

创建异步任务

通过解析MusicXML中的内容或使用预置曲目进行歌声的合成。您的应用可以通过调用此接口,创建异步执行任务,MusicXML支持版本3.1及以上。

获取access_token鉴权信息

使用开发者联盟界面获得的客户端ID以及对应密钥,发送HTTPS POST请求,获取查询access_token。获取方式请参见客户端模式(Client Credentials)。

创建异步任务

通过以上步骤获取的access_token信息,发送HTTPS POST创建歌声合成异步任务。

示例代码(Java)如下所示:

其中requestUrl = "https://audioeditor-api-drcn.cloud.huawei.com/v1/audioeditor/gateway/ai/ttsing/async/task/create"。

请点击下载MusicXML文件,并上传:

  /**
     * 调用创建异步任务接口
     * @throws Exception IO异常
     */
    private static void creatAsyncTask() throws Exception {
        
        // 设置请求header
        PostMethod postMethod = new PostMethod(requestUrl);
        // 设置文本类型(String),例:"application/json;charset=utf-8"
        postMethod.setRequestHeader("Content-Type", contentType);
        // 设置请求ID(String),例:"9af1aeda-531b-407a-80b4-65b40ef77bd6"
        postMethod.setRequestHeader("X-Request-ID", requestId);
        // 设置App包名(String),例:"com.huawei.demo"
        postMethod.setRequestHeader("X-Package-Name", pacageName);
        // 设置App所在国家(String),例:"cn"
        postMethod.setRequestHeader("X-Country-Code", countryCode);
        // 设置App标识,即Client ID(String),例:"123456789"
        postMethod.setRequestHeader("HMS-APPLICATION-ID", applicationId);
        // 设置证书指纹(String),例:"xxxxxxxxxxxxxxx"
        postMethod.setRequestHeader("certFingerprint", certFingerprint);
        // 设置动态获取的AccessToken(String)
        postMethod.setRequestHeader("Authorization","Bearer " + accessToken);

        // 设置请求body
        Map<String, Object> bodyMap = new HashMap<>();
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> configMap = new HashMap<>();
        // filePath是MusicXML文件路径(含文件名、后缀)
        String lyricFilePath = "filePath";
        dataMap.put("lyric", FileUtils.readFileToString(new File(lyricFilePath), "UTF-8"));
        dataMap.put("language", "chinese");
        configMap.put("type", 1);
        configMap.put("outputEncoderFormat", 0);
        configMap.put("wordDurationForceAlign", "false");
        bodyMap.put("data", dataMap);
        bodyMap.put("config", configMap);
        RequestEntity requestEntity = new StringRequestEntity(JSONObject.toJSONString(bodyMap),"application/json" ,"UTF-8");
        postMethod.setRequestEntity(requestEntity);

        HttpClient httpClient = new HttpClient();
        int ret = httpClient.executeMethod(postMethod);
        String rpsContent = postMethod.getResponseBodyAsString();
        if (ret == 200) {
            System.out.println(rpsContent);
        } else {
            System.out.println("callApi failed: ret =" + ret + " rsp=" + rpsContent);
        }
    }

使用预置曲目输入歌词:

/**
     * 调用创建异步任务接口
     * @throws Exception IO异常
     */
    private static void creatAsyncTask() throws Exception {
        
        // 设置请求header
        PostMethod postMethod = new PostMethod(requestUrl);
        // 设置文本类型(String),例:"application/json;charset=utf-8"
        postMethod.setRequestHeader("Content-Type", contentType);
        // 设置请求ID(String),例:"9af1aeda-531b-407a-80b4-65b40ef77bd6"
        postMethod.setRequestHeader("X-Request-ID", requestId);
        // 设置App包名(String),例:"com.huawei.demo"
        postMethod.setRequestHeader("X-Package-Name", pacageName);
        // 设置App所在国家(String),例:"cn"
        postMethod.setRequestHeader("X-Country-Code", countryCode);
        // 设置App标识,即Client ID(String),例:"123456789"
        postMethod.setRequestHeader("HMS-APPLICATION-ID", applicationId);
        // 设置证书指纹(String),例:"xxxxxxxxxxxxxxx"
        postMethod.setRequestHeader("certFingerprint", certFingerprint);
        // 设置动态获取的AccessToken(String)
        postMethod.setRequestHeader("Authorization","Bearer " + accessToken);
        // 设置请求body
        Map<String, Object> bodyMap = new HashMap<>();
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> configMap = new HashMap<>();
        String[] lyrics = {"跟随心跳的节拍", "感受自由的畅快", "把烦恼通通抛开", "我们一起嗨", "调整呼吸的节拍", "保持最好的状态", "奔向耀眼的未来", "哦康忙北北"};
        dataMap.put("lyrics", lyrics);
        dataMap.put("accompanimentId", "1");
        dataMap.put("isAutoFill", "false");
        dataMap.put("language", "chinese");
        configMap.put("type", 1);
        configMap.put("outputEncoderFormat", 0);
        configMap.put("wordDurationForceAlign", "false");
        bodyMap.put("data", dataMap);
        bodyMap.put("config", configMap);
        RequestEntity requestEntity = new StringRequestEntity(JSONObject.toJSONString(bodyMap),"application/json" ,"UTF-8");
        postMethod.setRequestEntity(requestEntity);

        HttpClient httpClient = new HttpClient();
        int ret = httpClient.executeMethod(postMethod);
        String rpsContent = postMethod.getResponseBodyAsString();
        if (ret == 200) {
            System.out.println(rpsContent);
        } else {
            System.out.println("callApi failed: ret =" + ret + " rsp=" + rpsContent);
        }
    }

上述代码中xxxxx对应的值请根据实际情况填写,具体取值请参见创建异步任务。