fix: 基本逻辑

This commit is contained in:
white 2024-10-16 18:55:30 +08:00
parent dc7b8bf343
commit 5be54dd239
5 changed files with 986 additions and 808 deletions

View File

@ -146,6 +146,10 @@
<!-- 讯飞语音识别 --> <!-- 讯飞语音识别 -->
<script> <script>
window.onload = function () { window.onload = function () {
reset();
};
function reset() {
var videoElement = document.getElementById('myVideo'); var videoElement = document.getElementById('myVideo');
var startTime = 5; // 开始时间(以秒为单位) var startTime = 5; // 开始时间(以秒为单位)
var endTime = 10; // 结束时间(以秒为单位) var endTime = 10; // 结束时间(以秒为单位)
@ -154,7 +158,9 @@
// 指定段落 // 指定段落
function playVideoSegment(startTime, endTime) { function playVideoSegment(startTime, endTime) {
videoElement.currentTime = startTime; videoElement.currentTime = startTime;
setTimeout(() => {
videoElement.play(); videoElement.play();
}, 50);
// 添加timeupdate事件监听器 // 添加timeupdate事件监听器
timeUpdateListener = function() { timeUpdateListener = function() {
@ -254,34 +260,37 @@
/* /*
* 实例化迅飞语音听写流式版WebAPI * 实例化迅飞语音听写流式版WebAPI
*/ */
var seconds = 10;
let seconds = 10; var gaptime = null;
const voice = new Voice({ const voice = new Voice({
// 服务接口认证信息 // 服务接口认证信息
appId: '5f4ffdeb', appId: '5f4ffdeb',
apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5', apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5',
apiKey: '0b17a761b6b7174b789f639119d7e29a', apiKey: '0b17a761b6b7174b789f639119d7e29a',
onWillStatusChange: function (oldStatus, newStatus) {}, onWillStatusChange: function (oldStatus, newStatus) {
console.log(oldStatus, newStatus, '交互逻辑')
},
onTextChange: function (text) { onTextChange: function (text) {
// 监听识别结果的变化 // 监听识别结果的变化
voiceTxt.innerText = text; voiceTxt.innerText = text;
marquee("marquee", "voice-txt"); marquee("marquee", "voice-txt");
let timer = setInterval(() => { // 如果已有定时器在运行,则清除它
if (gaptime) {
clearInterval(gaptime);
};
// 重置倒计时
seconds = 10;
console.log(text)
gaptime = setInterval(() => {
console.log(seconds, '倒计时')
seconds--; seconds--;
if (voiceTxt.innerText) {
if (seconds <= 0) { console.log('有 text 参数'); // 如果有 text 参数,打印 1
clearInterval(timer); clearInterval(gaptime);
if (text) { gaptime = null;
console.log(1); // 如果有 text 参数,打印 1
} else {
console.log("没有传入 text 参数"); // 如果没有 text 参数,执行其他逻辑
}
}
}, 1000);
// 3秒钟内没有说话就自动关闭 // 3秒钟内没有说话就自动关闭
if (text) { if (text) {
@ -290,7 +299,6 @@
clearTimeout(times); clearTimeout(times);
times = setTimeout(() => { times = setTimeout(() => {
this.stop(); this.stop();
statusTxt.value = '正在说话';
const params = { msg: text }; const params = { msg: text };
@ -466,6 +474,10 @@
}, 3000); }, 3000);
} }
} }
} else if (seconds == 0){
reset();
}
}, 1000);
} }
}); });
@ -506,15 +518,24 @@
function startShibie() { function startShibie() {
/**开始识别**/ /**开始识别**/
voiceTxt.innerText = ''; voiceTxt.innerText = '';
stopRec.textContent = '暂停'; stopRec.textContent = '暂停';
voice.start();
isCallbackExecuted = false; isCallbackExecuted = false;
// 先静音即可处理解决(提前做交互) // 先静音即可处理解决(提前做交互)
audioElement.muted = false; audioElement.muted = false;
audioElement.pause();
// 检查是否可以播放,避免中断
if (!audioElement.paused) {
audioElement.currentTime = 0; audioElement.currentTime = 0;
audioElement.pause();
};
seconds = 10;
voice.start();
startRec.style.display = 'none'; startRec.style.display = 'none';
endRec.style.display = 'none'; endRec.style.display = 'none';
@ -523,20 +544,28 @@
} }
function closeShibie() { function closeShibie() {
reset();
/**关闭识别**/ /**关闭识别**/
voiceTxt.innerText = ''; voiceTxt.innerText = '';
statusTxt.value = ''; statusTxt.value = '';
stopRec.textContent = '暂停'; stopRec.textContent = '暂停';
voice.stop();
// 音频 // 音频
audioElement.pause(); // 检查是否可以播放,避免中断
if (!audioElement.paused) {
audioElement.currentTime = 0; audioElement.currentTime = 0;
audioElement.pause();
};
// 视频 // 视频
replayVideoSegment(0, 60); replayVideoSegment(0, 60);
isCallbackExecuted = false; isCallbackExecuted = false;
seconds = 10;
voice.stop();
console.log('关闭识别');
startRec.style.display = 'block'; startRec.style.display = 'block';
endRec.style.display = 'none'; endRec.style.display = 'none';
@ -647,9 +676,11 @@
stopRec.style.display = 'none'; stopRec.style.display = 'none';
statusTxt.value = ''; statusTxt.value = '';
voiceTxt.innerText = ''; voiceTxt.innerText = '';
voice.stop();
replayVideoSegment(0, 60); replayVideoSegment(0, 60);
hideModal() hideModal()
// 播放结束重新开始监听
startShibie();
}); });
Subtitles = ""; Subtitles = "";
@ -748,7 +779,7 @@
// 如果没有找到匹配的数据项则返回null或错误信息这里选择返回null // 如果没有找到匹配的数据项则返回null或错误信息这里选择返回null
return null; return null;
} }
}; }
</script> </script>

View File

@ -146,6 +146,10 @@
<!-- 讯飞语音识别 --> <!-- 讯飞语音识别 -->
<script> <script>
window.onload = function () { window.onload = function () {
reset();
};
function reset() {
var videoElement = document.getElementById('myVideo'); var videoElement = document.getElementById('myVideo');
var startTime = 5; // 开始时间(以秒为单位) var startTime = 5; // 开始时间(以秒为单位)
var endTime = 10; // 结束时间(以秒为单位) var endTime = 10; // 结束时间(以秒为单位)
@ -154,7 +158,9 @@
// 指定段落 // 指定段落
function playVideoSegment(startTime, endTime) { function playVideoSegment(startTime, endTime) {
videoElement.currentTime = startTime; videoElement.currentTime = startTime;
setTimeout(() => {
videoElement.play(); videoElement.play();
}, 50);
// 添加timeupdate事件监听器 // 添加timeupdate事件监听器
timeUpdateListener = function() { timeUpdateListener = function() {
@ -254,18 +260,38 @@
/* /*
* 实例化迅飞语音听写流式版WebAPI * 实例化迅飞语音听写流式版WebAPI
*/ */
var seconds = 10;
var gaptime = null;
const voice = new Voice({ const voice = new Voice({
// 服务接口认证信息 // 服务接口认证信息
appId: '5f4ffdeb', appId: '5f4ffdeb',
apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5', apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5',
apiKey: '0b17a761b6b7174b789f639119d7e29a', apiKey: '0b17a761b6b7174b789f639119d7e29a',
onWillStatusChange: function (oldStatus, newStatus) {}, onWillStatusChange: function (oldStatus, newStatus) {
console.log(oldStatus, newStatus, '交互逻辑')
},
onTextChange: function (text) { onTextChange: function (text) {
// 监听识别结果的变化 // 监听识别结果的变化
voiceTxt.innerText = text; voiceTxt.innerText = text;
marquee("marquee", "voice-txt"); marquee("marquee", "voice-txt");
// 如果已有定时器在运行,则清除它
if (gaptime) {
clearInterval(gaptime);
};
// 重置倒计时
seconds = 10;
console.log(text)
gaptime = setInterval(() => {
console.log(seconds, '倒计时')
seconds--;
if (voiceTxt.innerText) {
console.log('有 text 参数'); // 如果有 text 参数,打印 1
clearInterval(gaptime);
gaptime = null;
// 3秒钟内没有说话就自动关闭 // 3秒钟内没有说话就自动关闭
if (text) { if (text) {
statusTxt.value = '正在听…'; statusTxt.value = '正在听…';
@ -449,6 +475,14 @@
}, 3000); }, 3000);
} }
} }
} else if (seconds == 0){
clearInterval(gaptime);
closeShibie();
reset();
gaptime = null;
seconds = 10;
}
}, 1000);
} }
}); });
@ -463,7 +497,7 @@
closeShibie(); closeShibie();
}); });
// 关闭识别 // 暂停识别
stopRec.addEventListener("click", function() { stopRec.addEventListener("click", function() {
stopPlay(); stopPlay();
}); });
@ -475,25 +509,38 @@
videoElement.play(); videoElement.play();
isPlaying = true; isPlaying = true;
stopRec.textContent = '暂停'; stopRec.textContent = '暂停';
statusTxt.value = '正在播放';
endRec.style.display = 'block';
} else { } else {
audioElement.pause(); audioElement.pause();
videoElement.pause(); videoElement.pause();
isPlaying = false; isPlaying = false;
stopRec.textContent = '继续播放'; stopRec.textContent = '继续播放';
statusTxt.value = '已暂停';
endRec.style.display = 'none';
} }
}; };
function startShibie() { function startShibie() {
/**开始识别**/ /**开始识别**/
voiceTxt.innerText = ''; voiceTxt.innerText = '';
stopRec.textContent = '暂停'; stopRec.textContent = '暂停';
voice.start();
isCallbackExecuted = false; isCallbackExecuted = false;
// 先静音即可处理解决(提前做交互) // 先静音即可处理解决(提前做交互)
audioElement.muted = false; audioElement.muted = false;
audioElement.pause();
// 检查是否可以播放,避免中断
if (!audioElement.paused) {
audioElement.currentTime = 0; audioElement.currentTime = 0;
audioElement.pause();
};
seconds = 10;
voice.start();
startRec.style.display = 'none'; startRec.style.display = 'none';
endRec.style.display = 'none'; endRec.style.display = 'none';
@ -507,15 +554,21 @@
statusTxt.value = ''; statusTxt.value = '';
stopRec.textContent = '暂停'; stopRec.textContent = '暂停';
voice.stop();
// 音频 // 音频
audioElement.pause(); // 检查是否可以播放,避免中断
if (!audioElement.paused) {
audioElement.currentTime = 0; audioElement.currentTime = 0;
audioElement.pause();
};
// 视频 // 视频
replayVideoSegment(0, 60); replayVideoSegment(0, 60);
isCallbackExecuted = false; isCallbackExecuted = false;
seconds = 10;
voice.stop();
console.log('关闭识别');
startRec.style.display = 'block'; startRec.style.display = 'block';
endRec.style.display = 'none'; endRec.style.display = 'none';
@ -602,6 +655,8 @@
const blob = new Blob([content_bytes], { type: 'audio/mp3' }); const blob = new Blob([content_bytes], { type: 'audio/mp3' });
const blobUrl = URL.createObjectURL(blob); const blobUrl = URL.createObjectURL(blob);
statusTxt.value = '正在说话';
// 字幕文字 // 字幕文字
voiceTxt.innerText = str; voiceTxt.innerText = str;
marquee("marquee", "voice-txt"); marquee("marquee", "voice-txt");
@ -621,11 +676,14 @@
// 停止倒计时 // 停止倒计时
startRec.style.display = 'block'; startRec.style.display = 'block';
endRec.style.display = 'none'; endRec.style.display = 'none';
stopRec.style.display = 'none';
statusTxt.value = ''; statusTxt.value = '';
voiceTxt.innerText = ''; voiceTxt.innerText = '';
voice.stop();
replayVideoSegment(0, 60); replayVideoSegment(0, 60);
hideModal() hideModal()
// 播放结束重新开始监听
startShibie();
}); });
Subtitles = ""; Subtitles = "";
@ -724,7 +782,7 @@
// 如果没有找到匹配的数据项则返回null或错误信息这里选择返回null // 如果没有找到匹配的数据项则返回null或错误信息这里选择返回null
return null; return null;
} }
}; }
</script> </script>

View File

@ -146,6 +146,10 @@
<!-- 讯飞语音识别 --> <!-- 讯飞语音识别 -->
<script> <script>
window.onload = function () { window.onload = function () {
reset();
};
function reset() {
var videoElement = document.getElementById('myVideo'); var videoElement = document.getElementById('myVideo');
var startTime = 5; // 开始时间(以秒为单位) var startTime = 5; // 开始时间(以秒为单位)
var endTime = 10; // 结束时间(以秒为单位) var endTime = 10; // 结束时间(以秒为单位)
@ -154,7 +158,9 @@
// 指定段落 // 指定段落
function playVideoSegment(startTime, endTime) { function playVideoSegment(startTime, endTime) {
videoElement.currentTime = startTime; videoElement.currentTime = startTime;
setTimeout(() => {
videoElement.play(); videoElement.play();
}, 50);
// 添加timeupdate事件监听器 // 添加timeupdate事件监听器
timeUpdateListener = function() { timeUpdateListener = function() {
@ -254,18 +260,38 @@
/* /*
* 实例化迅飞语音听写流式版WebAPI * 实例化迅飞语音听写流式版WebAPI
*/ */
var seconds = 10;
var gaptime = null;
const voice = new Voice({ const voice = new Voice({
// 服务接口认证信息 // 服务接口认证信息
appId: '5f4ffdeb', appId: '5f4ffdeb',
apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5', apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5',
apiKey: '0b17a761b6b7174b789f639119d7e29a', apiKey: '0b17a761b6b7174b789f639119d7e29a',
onWillStatusChange: function (oldStatus, newStatus) {}, onWillStatusChange: function (oldStatus, newStatus) {
console.log(oldStatus, newStatus, '交互逻辑')
},
onTextChange: function (text) { onTextChange: function (text) {
// 监听识别结果的变化 // 监听识别结果的变化
voiceTxt.innerText = text; voiceTxt.innerText = text;
marquee("marquee", "voice-txt"); marquee("marquee", "voice-txt");
// 如果已有定时器在运行,则清除它
if (gaptime) {
clearInterval(gaptime);
};
// 重置倒计时
seconds = 10;
console.log(text)
gaptime = setInterval(() => {
console.log(seconds, '倒计时')
seconds--;
if (voiceTxt.innerText) {
console.log('有 text 参数'); // 如果有 text 参数,打印 1
clearInterval(gaptime);
gaptime = null;
// 3秒钟内没有说话就自动关闭 // 3秒钟内没有说话就自动关闭
if (text) { if (text) {
statusTxt.value = '正在听…'; statusTxt.value = '正在听…';
@ -449,6 +475,14 @@
}, 3000); }, 3000);
} }
} }
} else if (seconds == 0){
clearInterval(gaptime);
closeShibie();
reset();
gaptime = null;
seconds = 10;
}
}, 1000);
} }
}); });
@ -463,7 +497,7 @@
closeShibie(); closeShibie();
}); });
// 关闭识别 // 暂停识别
stopRec.addEventListener("click", function() { stopRec.addEventListener("click", function() {
stopPlay(); stopPlay();
}); });
@ -475,25 +509,38 @@
videoElement.play(); videoElement.play();
isPlaying = true; isPlaying = true;
stopRec.textContent = '暂停'; stopRec.textContent = '暂停';
statusTxt.value = '正在播放';
endRec.style.display = 'block';
} else { } else {
audioElement.pause(); audioElement.pause();
videoElement.pause(); videoElement.pause();
isPlaying = false; isPlaying = false;
stopRec.textContent = '继续播放'; stopRec.textContent = '继续播放';
statusTxt.value = '已暂停';
endRec.style.display = 'none';
} }
}; };
function startShibie() { function startShibie() {
/**开始识别**/ /**开始识别**/
voiceTxt.innerText = ''; voiceTxt.innerText = '';
stopRec.textContent = '暂停'; stopRec.textContent = '暂停';
voice.start();
isCallbackExecuted = false; isCallbackExecuted = false;
// 先静音即可处理解决(提前做交互) // 先静音即可处理解决(提前做交互)
audioElement.muted = false; audioElement.muted = false;
audioElement.pause();
// 检查是否可以播放,避免中断
if (!audioElement.paused) {
audioElement.currentTime = 0; audioElement.currentTime = 0;
audioElement.pause();
};
seconds = 10;
voice.start();
startRec.style.display = 'none'; startRec.style.display = 'none';
endRec.style.display = 'none'; endRec.style.display = 'none';
@ -507,15 +554,21 @@
statusTxt.value = ''; statusTxt.value = '';
stopRec.textContent = '暂停'; stopRec.textContent = '暂停';
voice.stop();
// 音频 // 音频
audioElement.pause(); // 检查是否可以播放,避免中断
if (!audioElement.paused) {
audioElement.currentTime = 0; audioElement.currentTime = 0;
audioElement.pause();
};
// 视频 // 视频
replayVideoSegment(0, 60); replayVideoSegment(0, 60);
isCallbackExecuted = false; isCallbackExecuted = false;
seconds = 10;
voice.stop();
console.log('关闭识别');
startRec.style.display = 'block'; startRec.style.display = 'block';
endRec.style.display = 'none'; endRec.style.display = 'none';
@ -602,6 +655,8 @@
const blob = new Blob([content_bytes], { type: 'audio/mp3' }); const blob = new Blob([content_bytes], { type: 'audio/mp3' });
const blobUrl = URL.createObjectURL(blob); const blobUrl = URL.createObjectURL(blob);
statusTxt.value = '正在说话';
// 字幕文字 // 字幕文字
voiceTxt.innerText = str; voiceTxt.innerText = str;
marquee("marquee", "voice-txt"); marquee("marquee", "voice-txt");
@ -621,11 +676,14 @@
// 停止倒计时 // 停止倒计时
startRec.style.display = 'block'; startRec.style.display = 'block';
endRec.style.display = 'none'; endRec.style.display = 'none';
stopRec.style.display = 'none';
statusTxt.value = ''; statusTxt.value = '';
voiceTxt.innerText = ''; voiceTxt.innerText = '';
voice.stop();
replayVideoSegment(0, 60); replayVideoSegment(0, 60);
hideModal() hideModal()
// 播放结束重新开始监听
startShibie();
}); });
Subtitles = ""; Subtitles = "";
@ -724,7 +782,7 @@
// 如果没有找到匹配的数据项则返回null或错误信息这里选择返回null // 如果没有找到匹配的数据项则返回null或错误信息这里选择返回null
return null; return null;
} }
}; }
</script> </script>

View File

@ -146,6 +146,10 @@
<!-- 讯飞语音识别 --> <!-- 讯飞语音识别 -->
<script> <script>
window.onload = function () { window.onload = function () {
reset();
};
function reset() {
var videoElement = document.getElementById('myVideo'); var videoElement = document.getElementById('myVideo');
var startTime = 5; // 开始时间(以秒为单位) var startTime = 5; // 开始时间(以秒为单位)
var endTime = 10; // 结束时间(以秒为单位) var endTime = 10; // 结束时间(以秒为单位)
@ -154,7 +158,9 @@
// 指定段落 // 指定段落
function playVideoSegment(startTime, endTime) { function playVideoSegment(startTime, endTime) {
videoElement.currentTime = startTime; videoElement.currentTime = startTime;
setTimeout(() => {
videoElement.play(); videoElement.play();
}, 50);
// 添加timeupdate事件监听器 // 添加timeupdate事件监听器
timeUpdateListener = function() { timeUpdateListener = function() {
@ -254,34 +260,37 @@
/* /*
* 实例化迅飞语音听写流式版WebAPI * 实例化迅飞语音听写流式版WebAPI
*/ */
var seconds = 10;
let seconds = 10; var gaptime = null;
const voice = new Voice({ const voice = new Voice({
// 服务接口认证信息 // 服务接口认证信息
appId: '5f4ffdeb', appId: '5f4ffdeb',
apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5', apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5',
apiKey: '0b17a761b6b7174b789f639119d7e29a', apiKey: '0b17a761b6b7174b789f639119d7e29a',
onWillStatusChange: function (oldStatus, newStatus) {}, onWillStatusChange: function (oldStatus, newStatus) {
console.log(oldStatus, newStatus, '交互逻辑')
},
onTextChange: function (text) { onTextChange: function (text) {
// 监听识别结果的变化 // 监听识别结果的变化
voiceTxt.innerText = text; voiceTxt.innerText = text;
marquee("marquee", "voice-txt"); marquee("marquee", "voice-txt");
let timer = setInterval(() => { // 如果已有定时器在运行,则清除它
if (gaptime) {
clearInterval(gaptime);
};
// 重置倒计时
seconds = 10;
console.log(text)
gaptime = setInterval(() => {
console.log(seconds, '倒计时')
seconds--; seconds--;
if (voiceTxt.innerText) {
if (seconds <= 0) { console.log('有 text 参数'); // 如果有 text 参数,打印 1
clearInterval(timer); clearInterval(gaptime);
if (text) { gaptime = null;
console.log(1); // 如果有 text 参数,打印 1
} else {
console.log("没有传入 text 参数"); // 如果没有 text 参数,执行其他逻辑
}
}
}, 1000);
// 3秒钟内没有说话就自动关闭 // 3秒钟内没有说话就自动关闭
if (text) { if (text) {
@ -290,7 +299,6 @@
clearTimeout(times); clearTimeout(times);
times = setTimeout(() => { times = setTimeout(() => {
this.stop(); this.stop();
statusTxt.value = '正在说话';
const params = { msg: text }; const params = { msg: text };
@ -466,6 +474,10 @@
}, 3000); }, 3000);
} }
} }
} else if (seconds == 0){
reset();
}
}, 1000);
} }
}); });
@ -506,15 +518,24 @@
function startShibie() { function startShibie() {
/**开始识别**/ /**开始识别**/
voiceTxt.innerText = ''; voiceTxt.innerText = '';
stopRec.textContent = '暂停'; stopRec.textContent = '暂停';
voice.start();
isCallbackExecuted = false; isCallbackExecuted = false;
// 先静音即可处理解决(提前做交互) // 先静音即可处理解决(提前做交互)
audioElement.muted = false; audioElement.muted = false;
audioElement.pause();
// 检查是否可以播放,避免中断
if (!audioElement.paused) {
audioElement.currentTime = 0; audioElement.currentTime = 0;
audioElement.pause();
};
seconds = 10;
voice.start();
startRec.style.display = 'none'; startRec.style.display = 'none';
endRec.style.display = 'none'; endRec.style.display = 'none';
@ -523,20 +544,28 @@
} }
function closeShibie() { function closeShibie() {
reset();
/**关闭识别**/ /**关闭识别**/
voiceTxt.innerText = ''; voiceTxt.innerText = '';
statusTxt.value = ''; statusTxt.value = '';
stopRec.textContent = '暂停'; stopRec.textContent = '暂停';
voice.stop();
// 音频 // 音频
audioElement.pause(); // 检查是否可以播放,避免中断
if (!audioElement.paused) {
audioElement.currentTime = 0; audioElement.currentTime = 0;
audioElement.pause();
};
// 视频 // 视频
replayVideoSegment(0, 60); replayVideoSegment(0, 60);
isCallbackExecuted = false; isCallbackExecuted = false;
seconds = 10;
voice.stop();
console.log('关闭识别');
startRec.style.display = 'block'; startRec.style.display = 'block';
endRec.style.display = 'none'; endRec.style.display = 'none';
@ -647,9 +676,11 @@
stopRec.style.display = 'none'; stopRec.style.display = 'none';
statusTxt.value = ''; statusTxt.value = '';
voiceTxt.innerText = ''; voiceTxt.innerText = '';
voice.stop();
replayVideoSegment(0, 60); replayVideoSegment(0, 60);
hideModal() hideModal()
// 播放结束重新开始监听
startShibie();
}); });
Subtitles = ""; Subtitles = "";
@ -748,7 +779,7 @@
// 如果没有找到匹配的数据项则返回null或错误信息这里选择返回null // 如果没有找到匹配的数据项则返回null或错误信息这里选择返回null
return null; return null;
} }
}; }
</script> </script>