|
@@ -9,8 +9,107 @@ import { ModalController } from '@ionic/angular';
|
|
|
})
|
|
|
export class PageYuyinComponent implements OnInit {
|
|
|
|
|
|
- constructor() { }
|
|
|
-
|
|
|
ngOnInit() {}
|
|
|
|
|
|
+ recognition: any;
|
|
|
+ recognizedContent: string = '';
|
|
|
+ timer: string = '00:00';
|
|
|
+ interval: any;
|
|
|
+ elapsedSeconds: number = 0;
|
|
|
+ isVoiceInputVisible: boolean = false;
|
|
|
+
|
|
|
+
|
|
|
+ constructor() {
|
|
|
+
|
|
|
+ this.initSpeechRecognition();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ initSpeechRecognition() {
|
|
|
+ const SpeechRecognition = (window as any).SpeechRecognition || (window as any).webkitSpeechRecognition;
|
|
|
+ if (SpeechRecognition) {
|
|
|
+ this.recognition = new SpeechRecognition();
|
|
|
+ this.recognition.lang = 'zh-CN';
|
|
|
+ this.recognition.interimResults = false;
|
|
|
+ this.recognition.maxAlternatives = 1;
|
|
|
+ this.recognition.continuous = true;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ this.recognition.onresult = (event: any) => {
|
|
|
+ this.recognizedContent += event.results[event.results.length - 1][0].transcript;
|
|
|
+ console.log("识别到的内容:", this.recognizedContent);
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ this.recognition.onerror = (event: any) => {
|
|
|
+ if (event.error === 'no-speech') {
|
|
|
+ console.warn('没有检测到语音,继续监听...');
|
|
|
+ } else {
|
|
|
+ console.error('语音识别错误:', event.error);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ } else {
|
|
|
+ console.log('该浏览器不支持语音识别');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ startVoice() {
|
|
|
+ if (this.recognition && this.recognition.state !== 'active') {
|
|
|
+ this.recognition.start();
|
|
|
+ console.log('语音识别启动...');
|
|
|
+ this.startTimer();
|
|
|
+ this.isVoiceInputVisible = true;
|
|
|
+ } else {
|
|
|
+ console.warn('语音识别已经在运行中');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ startTimer() {
|
|
|
+ this.elapsedSeconds = 0;
|
|
|
+ this.timer = '00:00';
|
|
|
+ this.interval = setInterval(() => {
|
|
|
+ this.elapsedSeconds++;
|
|
|
+ const minutes = Math.floor(this.elapsedSeconds / 60);
|
|
|
+ const seconds = this.elapsedSeconds % 60;
|
|
|
+ this.timer = `${this.padZero(minutes)}:${this.padZero(seconds)}`;
|
|
|
+ }, 1000);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ padZero(num: number): string {
|
|
|
+ return num < 10 ? '0' + num : num.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ cancelVoiceInput() {
|
|
|
+ if (this.recognition) {
|
|
|
+ this.recognition.stop();
|
|
|
+ console.log('语音识别已停止');
|
|
|
+ clearInterval(this.interval);
|
|
|
+ this.timer = '00:00';
|
|
|
+ this.recognizedContent = '';
|
|
|
+ }
|
|
|
+ this.isVoiceInputVisible = false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ sendVoiceInput() {
|
|
|
+ if (this.recognition) {
|
|
|
+ this.recognition.stop();
|
|
|
+ console.log('语音识别已停止');
|
|
|
+ clearInterval(this.interval);
|
|
|
+ this.timer = '00:00';
|
|
|
+
|
|
|
+return this.recognizedContent;
|
|
|
+ }
|
|
|
+ this.isVoiceInputVisible = false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|