fix: 按钮状态

This commit is contained in:
white 2024-10-16 10:23:07 +08:00
parent 1674bbee2e
commit dc7b8bf343
7 changed files with 120 additions and 10 deletions

View File

@ -113,7 +113,7 @@
// 遍历每一行来找到 data 字段 // 遍历每一行来找到 data 字段
lines.forEach(line => { lines.forEach(line => {
if (line.trim().startsWith('data:')) { if (line.trim().startsWith('data:')) {
isDataLine = true; isDataLine = true;
dataLines.push(line.trim().slice(5)); // 移除 'data:' 前缀 dataLines.push(line.trim().slice(5)); // 移除 'data:' 前缀
} else if (isDataLine && !line.trim()) { } else if (isDataLine && !line.trim()) {
@ -254,6 +254,8 @@
/* /*
* 实例化迅飞语音听写流式版WebAPI * 实例化迅飞语音听写流式版WebAPI
*/ */
let seconds = 10;
const voice = new Voice({ const voice = new Voice({
// 服务接口认证信息 // 服务接口认证信息
appId: '5f4ffdeb', appId: '5f4ffdeb',
@ -266,6 +268,21 @@
voiceTxt.innerText = text; voiceTxt.innerText = text;
marquee("marquee", "voice-txt"); marquee("marquee", "voice-txt");
let timer = setInterval(() => {
seconds--;
if (seconds <= 0) {
clearInterval(timer);
if (text) {
console.log(1); // 如果有 text 参数,打印 1
} else {
console.log("没有传入 text 参数"); // 如果没有 text 参数,执行其他逻辑
}
}
}, 1000);
// 3秒钟内没有说话就自动关闭 // 3秒钟内没有说话就自动关闭
if (text) { if (text) {
statusTxt.value = '正在听…'; statusTxt.value = '正在听…';
@ -463,7 +480,7 @@
closeShibie(); closeShibie();
}); });
// 关闭识别 // 暂停识别
stopRec.addEventListener("click", function() { stopRec.addEventListener("click", function() {
stopPlay(); stopPlay();
}); });
@ -475,11 +492,15 @@
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';
} }
}; };
@ -602,6 +623,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,6 +644,7 @@
// 停止倒计时 // 停止倒计时
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(); voice.stop();

View File

@ -46,8 +46,11 @@
<!-- 录制 --> <!-- 录制 -->
<div class="buttons startRec">点击说话</div> <div class="buttons startRec">点击说话</div>
<!-- 暂停 -->
<div class="buttons endRec">中止</div>
<!-- 停止 --> <!-- 停止 -->
<div class="buttons endRec">停止</div> <div class="buttons stopRec">暂停</div>
</div> </div>
<!-- 讯飞测试 --> <!-- 讯飞测试 -->
@ -185,6 +188,7 @@
// 获取 audio 元素的引用 // 获取 audio 元素的引用
var audioElement = document.getElementById('myAudio'); var audioElement = document.getElementById('myAudio');
var isPlaying = false; // 是否播放
audioElement.muted = true; // 先静音 audioElement.muted = true; // 先静音
// 获取页面元素 // 获取页面元素
@ -196,7 +200,11 @@
// 点击事件 // 点击事件
var startRec = document.getElementsByClassName('startRec')[0]; var startRec = document.getElementsByClassName('startRec')[0];
var endRec = document.getElementsByClassName('endRec')[0]; var endRec = document.getElementsByClassName('endRec')[0];
var stopRec = document.getElementsByClassName('stopRec')[0];
endRec.style.display = "none"; endRec.style.display = "none";
stopRec.style.display = "none";
var token = null; var token = null;
let times = null; let times = null;
@ -450,15 +458,35 @@
startShibie(); startShibie();
}); });
// 关闭识别 // 关闭识别
endRec.addEventListener("click", function() { endRec.addEventListener("click", function() {
closeShibie(); closeShibie();
}); });
// 关闭识别
stopRec.addEventListener("click", function() {
stopPlay();
});
function stopPlay() {
/**暂停播放**/
if (audioElement.paused) {
audioElement.play();
videoElement.play();
isPlaying = true;
stopRec.textContent = '暂停';
} else {
audioElement.pause();
videoElement.pause();
isPlaying = false;
stopRec.textContent = '继续播放';
}
};
function startShibie() { function startShibie() {
/**开始识别**/ /**开始识别**/
voiceTxt.innerText = ''; voiceTxt.innerText = '';
stopRec.textContent = '暂停';
voice.start(); voice.start();
isCallbackExecuted = false; isCallbackExecuted = false;
@ -469,6 +497,7 @@
startRec.style.display = 'none'; startRec.style.display = 'none';
endRec.style.display = 'none'; endRec.style.display = 'none';
stopRec.style.display = 'none';
showModal(); showModal();
} }
@ -476,6 +505,7 @@
/**关闭识别**/ /**关闭识别**/
voiceTxt.innerText = ''; voiceTxt.innerText = '';
statusTxt.value = ''; statusTxt.value = '';
stopRec.textContent = '暂停';
voice.stop(); voice.stop();
@ -489,10 +519,10 @@
startRec.style.display = 'block'; startRec.style.display = 'block';
endRec.style.display = 'none'; endRec.style.display = 'none';
stopRec.style.display = 'none';
hideModal() hideModal()
} }
// 显示弹窗和遮罩 // 显示弹窗和遮罩
function showModal() { function showModal() {
modal.style.display = 'block'; modal.style.display = 'block';
@ -557,6 +587,7 @@
}).then(async(response) => { }).then(async(response) => {
startRec.style.display = "none"; startRec.style.display = "none";
endRec.style.display = "block"; endRec.style.display = "block";
stopRec.style.display = "block";
NextPlayVideo(response, filterString(Subtitles, ['*', ' '])); NextPlayVideo(response, filterString(Subtitles, ['*', ' ']));
}).catch(e => { }).catch(e => {

View File

@ -46,8 +46,11 @@
<!-- 录制 --> <!-- 录制 -->
<div class="buttons startRec">点击说话</div> <div class="buttons startRec">点击说话</div>
<!-- 暂停 -->
<div class="buttons endRec">中止</div>
<!-- 停止 --> <!-- 停止 -->
<div class="buttons endRec">停止</div> <div class="buttons stopRec">暂停</div>
</div> </div>
<!-- 讯飞测试 --> <!-- 讯飞测试 -->
@ -185,6 +188,7 @@
// 获取 audio 元素的引用 // 获取 audio 元素的引用
var audioElement = document.getElementById('myAudio'); var audioElement = document.getElementById('myAudio');
var isPlaying = false; // 是否播放
audioElement.muted = true; // 先静音 audioElement.muted = true; // 先静音
// 获取页面元素 // 获取页面元素
@ -196,7 +200,11 @@
// 点击事件 // 点击事件
var startRec = document.getElementsByClassName('startRec')[0]; var startRec = document.getElementsByClassName('startRec')[0];
var endRec = document.getElementsByClassName('endRec')[0]; var endRec = document.getElementsByClassName('endRec')[0];
var stopRec = document.getElementsByClassName('stopRec')[0];
endRec.style.display = "none"; endRec.style.display = "none";
stopRec.style.display = "none";
var token = null; var token = null;
let times = null; let times = null;
@ -450,15 +458,35 @@
startShibie(); startShibie();
}); });
// 关闭识别 // 关闭识别
endRec.addEventListener("click", function() { endRec.addEventListener("click", function() {
closeShibie(); closeShibie();
}); });
// 关闭识别
stopRec.addEventListener("click", function() {
stopPlay();
});
function stopPlay() {
/**暂停播放**/
if (audioElement.paused) {
audioElement.play();
videoElement.play();
isPlaying = true;
stopRec.textContent = '暂停';
} else {
audioElement.pause();
videoElement.pause();
isPlaying = false;
stopRec.textContent = '继续播放';
}
};
function startShibie() { function startShibie() {
/**开始识别**/ /**开始识别**/
voiceTxt.innerText = ''; voiceTxt.innerText = '';
stopRec.textContent = '暂停';
voice.start(); voice.start();
isCallbackExecuted = false; isCallbackExecuted = false;
@ -469,6 +497,7 @@
startRec.style.display = 'none'; startRec.style.display = 'none';
endRec.style.display = 'none'; endRec.style.display = 'none';
stopRec.style.display = 'none';
showModal(); showModal();
} }
@ -476,6 +505,7 @@
/**关闭识别**/ /**关闭识别**/
voiceTxt.innerText = ''; voiceTxt.innerText = '';
statusTxt.value = ''; statusTxt.value = '';
stopRec.textContent = '暂停';
voice.stop(); voice.stop();
@ -489,10 +519,10 @@
startRec.style.display = 'block'; startRec.style.display = 'block';
endRec.style.display = 'none'; endRec.style.display = 'none';
stopRec.style.display = 'none';
hideModal() hideModal()
} }
// 显示弹窗和遮罩 // 显示弹窗和遮罩
function showModal() { function showModal() {
modal.style.display = 'block'; modal.style.display = 'block';
@ -557,6 +587,7 @@
}).then(async(response) => { }).then(async(response) => {
startRec.style.display = "none"; startRec.style.display = "none";
endRec.style.display = "block"; endRec.style.display = "block";
stopRec.style.display = "block";
NextPlayVideo(response, filterString(Subtitles, ['*', ' '])); NextPlayVideo(response, filterString(Subtitles, ['*', ' ']));
}).catch(e => { }).catch(e => {

View File

@ -113,7 +113,7 @@
// 遍历每一行来找到 data 字段 // 遍历每一行来找到 data 字段
lines.forEach(line => { lines.forEach(line => {
if (line.trim().startsWith('data:')) { if (line.trim().startsWith('data:')) {
isDataLine = true; isDataLine = true;
dataLines.push(line.trim().slice(5)); // 移除 'data:' 前缀 dataLines.push(line.trim().slice(5)); // 移除 'data:' 前缀
} else if (isDataLine && !line.trim()) { } else if (isDataLine && !line.trim()) {
@ -254,6 +254,8 @@
/* /*
* 实例化迅飞语音听写流式版WebAPI * 实例化迅飞语音听写流式版WebAPI
*/ */
let seconds = 10;
const voice = new Voice({ const voice = new Voice({
// 服务接口认证信息 // 服务接口认证信息
appId: '5f4ffdeb', appId: '5f4ffdeb',
@ -266,6 +268,21 @@
voiceTxt.innerText = text; voiceTxt.innerText = text;
marquee("marquee", "voice-txt"); marquee("marquee", "voice-txt");
let timer = setInterval(() => {
seconds--;
if (seconds <= 0) {
clearInterval(timer);
if (text) {
console.log(1); // 如果有 text 参数,打印 1
} else {
console.log("没有传入 text 参数"); // 如果没有 text 参数,执行其他逻辑
}
}
}, 1000);
// 3秒钟内没有说话就自动关闭 // 3秒钟内没有说话就自动关闭
if (text) { if (text) {
statusTxt.value = '正在听…'; statusTxt.value = '正在听…';
@ -463,7 +480,7 @@
closeShibie(); closeShibie();
}); });
// 关闭识别 // 暂停识别
stopRec.addEventListener("click", function() { stopRec.addEventListener("click", function() {
stopPlay(); stopPlay();
}); });
@ -475,11 +492,15 @@
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';
} }
}; };
@ -602,6 +623,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,6 +644,7 @@
// 停止倒计时 // 停止倒计时
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(); voice.stop();

Binary file not shown.