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

View File

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

View File

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

View File

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