能不能给个阿里语音AI的js实时转写的demo呀?官网没看到demo,能不能帮忙找一个
当然可以帮您找一个阿里语音AI的实时转写的Demo。以下是一个基于阿里云语音实时识别服务的JavaScript Demo:
<!DOCTYPE html>
<html>
<head>
<title>阿里语音AI实时转写Demo</title>
</head>
<body>
<button onclick="startTranscription()">开始转写</button>
<button onclick="stopTranscription()">停止转写</button>
<br>
<textarea id="transcriptionResult" rows="5" cols="50" readonly></textarea>
<script src="//g.alicdn.com/dingding/dingtalk-pc-api/2.8.0/index.js"></script>
<script src="//g.alicdn.com/fdilab/lib3rd/aliyun-sdk/1.13.0/aliyun-sdk.min.js"></script>
<script>
var appKey = "YOUR_APP_KEY"; // 替换为您的阿里云AppKey
var appSecret = "YOUR_APP_SECRET"; // 替换为您的阿里云AppSecret
function getToken() {
return new Promise((resolve, reject) => {
DingTalkPC.ready(function () {
DingTalkPC.runtime.permission.requestAuthCode({
corpId: 'YOUR_CORP_ID', // 替换为您的钉钉企业ID
onSuccess: function (info) {
var authCode = info.code;
// 获取阿里云的Token
var client = new DYSMS({ AppKey: appKey, AppSecret: appSecret });
client.GetToken({ AuthCode: authCode }, function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
},
onFail: function (err) {
reject(err);
}
});
});
});
}
var audioContext;
var mediaStream;
var mediaRecorder;
var chunks = [];
var transcriptionResultElement;
function getUserMedia(constraints) {
if (navigator.mediaDevices.getUserMedia) {
return navigator.mediaDevices.getUserMedia(constraints);
}
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
if (getUserMedia) {
return new Promise(function (resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
}
throw new Error('不支持音频输入');
}
function startTranscription() {
getToken()
.then((tokenData) => {
if (!audioContext) {
audioContext = new AudioContext();
}
getUserMedia({ audio: true })
.then((stream) => {
mediaStream = stream;
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = handleDataAvailable;
mediaRecorder.start();
// 创建阿里云语音实时识别服务对象
var dyasr = new DYSMS({
AppKey: appKey,
Token: tokenData.Token,
Format: "pcm",
SampleRate: 16000,
OnRecognitionCompleted: function (err, result) {
if (err) {
console.error(err);
} else {
transcriptionResultElement.value += result + "\n";
}
}
});
// 开始实时转写
dyasr.Start();
})
.catch((error) => {
console.error(error);
});
})
.catch((error) => {
console.error(error);
});
}
function handleDataAvailable(event) {
if (event.data.size > 0) {
chunks.push(event.data);
}
}
function stopTranscription() {
mediaRecorder.stop();
mediaStream.getTracks().forEach(function (track) {
track.stop();
});
// 停止实时转写
var dyasr = new DYSMS({
AppKey: appKey
});
dyasr.Stop();
}
window.onload = function () {
transcriptionResultElement = document.getElementById("transcriptionResult");
};
</script>
</body>
</html>
请将以下值替换为您的实际信息:
YOUR_APP_KEY
:您的阿里云AppKeyYOUR_APP_SECRET
:您的阿里云AppSecretYOUR_CORP_ID
:您的钉钉企业ID在上述Demo中,我们使用了阿里
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。