sky999
天山茗客
UID 181291
Digest
2
Points 10
Posts 3869
码币MB 2619 Code
黄金 0 Catty
钻石 884 Pellet
Permissions 10
Register 2020-11-28
Status offline
|
语音识别程序 Version 1.x系列,无调用第三方库。
<!DOCTYPE html>
<html>
<head>
<?php session_start(); ?>
<title>语音输入</title>
</head>
<body>
<div id="chat-box"></div>
<button type="button" id="voice-button">录音</button>
<script>
function addMessage(author, message, time) {
var chatBox = document.getElementById('chat-box');
if (chatBox.childNodes.length === 0) {
var placeholderDiv = document.createElement('div');
placeholderDiv.innerText = '正在等待语音输入...';
chatBox.appendChild(placeholderDiv);
}
var lastMessageDiv = chatBox.lastChild;
if (lastMessageDiv && lastMessageDiv.innerText.startsWith('我:录音中')) {
chatBox.removeChild(lastMessageDiv);
}
var messageDiv = document.createElement('div');
messageDiv.innerHTML = '<strong>' + author + '</strong> (' + time + '): ' + message;
chatBox.appendChild(messageDiv);
chatBox.scrollTop = chatBox.scrollHeight;
}
var recognition = null;
var isRecording = false;
var audioChunks = [];
var startTime = null;
if ('webkitSpeechRecognition' in window) {
recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function() {
isRecording = true;
startTime = Date.now();
addMessage('我', '录音中...', '');
};
recognition.onend = function() {
isRecording = false;
var chatBox = document.getElementById('chat-box');
var lastMessageDiv = chatBox.lastChild;
if (lastMessageDiv && lastMessageDiv.innerText.startsWith('我:录音中')) {
chatBox.removeChild(lastMessageDiv);
}
if (audioChunks.length > 0) {
var blob = new Blob(audioChunks, { type: 'audio/webm' });
var audioUrl = URL.createObjectURL(blob);
var audio = new Audio(audioUrl);
recognition.onresult = null;
// Play the recorded audio and then start a new recognition session
audio.addEventListener('canplaythrough', function() {
var author = '<?php echo $_SESSION['Mud_username'];?>';
var time = new Date().toLocaleString();
addMessage(author, '<a href="' + audioUrl + '" target="_blank">点击播放</a>', time);
recognition.start();
});
audio.play();
} else {
recognition.onresult = null;
recognition.start();
}
audioChunks = [];
};
recognition.onerror = function(err) {
console.error(err);
isRecording = false;
};
recognition.onresult = function(event) {
var result = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results.isFinal) {
result += event.results[0].transcript;
}
}
result = result.replace(/[\s\r\n]+/g, ' ').trim();
if (result !== '') {
var author = '<?php echo $_SESSION['Mud_username'];?>';
addMessage(author, result, '');
var formData = new FormData();
formData.append('text', result);
formData.append('author', author);
formData.append('time', new Date().toISOString());
var xhr = new XMLHttpRequest();
xhr.open('POST', 'save.php');
xhr.onload = function() {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.error('请求失败');
}
};
xhr.send(formData);
audioChunks = [];
}
};
document.getElementById('voice-button').addEventListener('click', function() {
if (isRecording) {
recognition.stop();
} else {
recognition.start();
}
});
} else {
alert('浏览器不支持语音输入');
}
</script>
</body>
</html>
|  CAFFZ.com
|
|