Skip to content

Latest commit

 

History

History
110 lines (92 loc) · 2.8 KB

README.md

File metadata and controls

110 lines (92 loc) · 2.8 KB

web-rtvt-sdk 使用文档

引入依赖库

<script src="libs/msgpack.min.js"></script>
<script src="libs/md5.min.js"></script>
<script src="libs/int64.min.js"></script>
<script src="libs/crypto-js.min.js"></script>
<script src="libs/fpnn.min.js"></script>
<script src="dist/rtvt.sdk.min.js"></script>

使用示例

let rtvtClient = new RTVTClient({
    endpoint: 'wss://rtvt.ilivedata.com:14002',  // endpoint由控制台获取
    pid: pid,   // pid由控制台获取
    uid: uid,  // uid
});

// 发生自动重连时触发
rtvtClient.on("ReloginCompleted", function(ok, errorCode) {
    console.log("ReloginCompleted, ok: " + ok + " errorCode: " + errorCode);
});

// 内部错误采集
rtvtClient.on("ErrorRecorder", function(error) {
    console.log(error);
});

/* 
登录
token: 生成方式见下文
ts: 与生成token时使用的ts一致
*/
rtvtClient.login(token, ts, function(ok, errorCode) {
    if (!ok) {
        console.log("login fail: " + errorCode);
        return;
    }

    // 创建流
    rtvtClient.createStream("zh", "en", true, true, true, function(stream, errorCode) {
        if (stream == null) {
            console.log("create stream fail: " + errorCode);
            return;
        }

        // 有识别结果时触发,data具体格式见下文
        rtvtClient.on("recognizedResult", function(data) {
            console.log(data);
        });

        // 有翻译结果时触发,data具体格式见下文
        rtvtClient.on("translatedResult", function(data) {
            console.log(data);
        });

        // 有临时识别结果时触发,data具体格式见下文
        rtvtClient.on("recognizedTempResult", function(data) {
            console.log(data);
        });

        // 有临时翻译结果时触发,data具体格式见下文
        rtvtClient.on("translatedTempResult", function(data) {
            console.log(data);
        });

        // 发送音频PCM数据, 要求16000采样率 单声道 固定640字节,seq为语音片段序号(尽量有序)
        rtvtClient.sendVoice(stream, seq, pcm);
    });
});

token生成方式

var pid = 81700001;
var key = 'xxxxx-xxxx-xxxx-xxxx-xxxxx';
var ts = parseInt(new Date().getTime() / 1000);
var coreString = pid + ":" + ts;
var token = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(coreString, atob(key)));

识别与翻译结果推送结构示例

// 识别
{
    "pid": 81700001,
    "streamId": 1303369879658692600,
    "startTs": 1669603280951,
    "endTs": 1669603286480,
    "recTs": 1669603286864,
    "asr": "喂喂"
}

// 翻译
{
    "pid": 81700001,
    "streamId": 1303369879658692600,
    "startTs": 1669603280951,
    "endTs": 1669603286480,
    "trans": "Hey, hey",
    "recTs": 1669603286864,
    "lang": "en"
}