๐ถโ๏ธSpeech Recognition
Speech Recognition์ด๋ ์ฌ๋์ด ๋งํ๋ ์์ฑ์ธ์ด๋ฅผ ์ปดํจํฐ๊ฐ ํด์ํด ๋ด์ฉ์ ๋ฌธ์ ๋ฐ์ดํฐ๋ก ์ ํํ๋ ์ฒ๋ฆฌ๋ฅผ ๋งํ๋ค๊ณ
์ํค๋ฐฑ๊ณผ์ ์ ํ์๋ค.
์๋ ์ฌ์ง์ฒ๋ผ ๋ด๊ฐ ๋งํ ์์ฑ์ ๋ฐ์ดํฐ๋ก ๋ด ๋ณด๋ด์ฃผ๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.

๐Ionic Speech Recognition Plugin ์ค์น
๊ธฐ๋ณธ์ ์ผ๋ก ๊ตฌ๊ธ Speech Recognition๋ฅผ ์ฌ์ฉํ๋ค. ๊ทธ๋ฌ๊ธฐ ์ํด์๋ Plugin ์ค์น๋ฅผ ํ์๋ก ํ๋ค. ๋๋ ํ์ฌ Ionic + Cordova๋ก ์ฌ์ฉํ์๋ค.
$ ionic cordova plugin add cordova-plugin-speechrecognition
$ npm install @awesome-cordova-plugins/speech-recognition
์ ์ค์น๋ฅผ ํ๊ณ ๋์ ํ์ฌ ๊ธฐ์ค์ผ๋ก config.xml์ ์๋ ๋ฌธ๊ตฌ๋ฅผ ์ถ๊ฐํด์ค์ผ ํ๋ค.
<config-file parent="/manifest" target="AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<queries>
<intent>
<action android:name="android.speech.RecognitionService" />
</intent>
</queries>
</config-file>

๐ฅIonic ์ ์ฉํ๊ธฐ
์ฌ์ฉํ ๋ณ์
- isRecording : ๋ น์์ํ ํ์
- options : speech Recognition ์ต์
ํ์ฌ ๋ด๊ฐ ์ฌ์ฉํ๊ณ ์๋ ์ต์ ์ 3๊ฐ์ง๋ฅผ ์ฌ์ฉํ์๋ค.
- language : ์์ฑ์ธ์์ ์์๋ค์ ์ธ์ด
- showPopup : ํ์ ์ค์ ์ฌ๋ถ true๋ฅผ ํ๋ฉด ๊ตฌ๊ธ speech recongnition์ด ๋ฌ๋ค
- showPartial : ์ค์๊ฐ์ผ๋ก ์์ฑ์ ๋ฐ์๋ค์ผ์ง ์ ํํ๋ ๊ฒ์ผ๋ก ๋ณด์ธ๋ค. false๋ก ํ๋ฉด ํ ๋ฌธ์ฅ์ ๋งํ๊ณ ๋์, ๊ฒฐ๊ณผ๋ฅผ ์ค๋ค.
์ฌ์ฉํ ํจ์ ๊ธฐ๋ฅ
- recording : ๋ น์์ ํ๋ ๊ธฐ๋ฅ์ด๋ฉฐ, subscribe์์ ์ฑ๊ณตํ๋ฉด matches๋ฅผ ๋ฐ๊ณ , ์คํจํ๋ฉด error๋ฅผ ๋ฐ๋๋ค.
- stopRecording : ๋ น์์ ์ค์งํ๋ ๊ธฐ๋ฅ์ด๋ค.
import { Component, ChangeDetectorRef } from '@angular/core';
import { SpeechRecognition } from '@awesome-cordova-plugins/speech-recognition/ngx';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
isRecording: boolean = false;
options = {
language: 'en-US',
showPopup: false,
showPartial: true
}
constructor(private speechRecognition: SpeechRecognition, private cd: ChangeDetectorRef) {}
recording() {
this.isRecording = true;
this.speechRecognition.startListening(this.options)
.subscribe(
(matches) => {
console.log(`success : `, matches);
this.isRecording = false;
this.cd.detectChanges();
},
(error) => {
console.error(`recording : `, error);
this.isRecording = false;
this.cd.detectChanges();
})
}
stopRecording() {
this.isRecording = false;
this.speechRecognition.stopListening().then((value) => {
console.log('value : ' , value);
})
}
}
์๋๋ HTML ์ด๋ค
isRecording์ด true์ด๋ฉด ๋ถ์์์ผ๋ก, isRecording์ด false์ด๋ฉด ํธ๋ฅธ์์ผ๋ก ๋ณด์ฌ์ฃผ๊ธฐ๋ก ํ์๋ค.
<ion-header [translucent]="true">
<ion-toolbar [color]="isRecording ? 'danger' : 'primary'">
<ion-title>
Blank
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-button expand="block" (click)="recording()">๋
น์</ion-button>
<ion-button expand="block" (click)="stopRecording()">๋
น์์ค์ง</ion-button>
</ion-content>
๐ค ์ ์ฉ ๊ฒฐ๊ณผ
์๋๋ ๋ น์์ ํ๊ธฐ์ ์ฌ์ง์ด๋ค.

๋ น์์ค์ผ๋๋ ๋ถ์์ ํ์๋ฅผ ํ์๋ค.

๊ทธ๋ฆฌ๊ณ ๋ น์๊ฒฐ๊ณผ๋ฅผ ์๋ ์ฌ์ง์ฒ๋ผ ๋ฐ์ ์ ์๋ค.

์ ์ฌ์ง ์ฒ๋ผ success์ ๋์จ ๊ฒ์ด ๋ด๊ฐ ๋งํ ๊ฒฐ๊ณผ๋ฌผ์ด๋ค.
๐บ ํ๊ธฐ
ํ์ฌ์์ ์ ์ฉํ ๊ฒ์ ์ด๊ฒ๋ณด๋ค๋ ๋ณต์กํ๋ค. ์ค์ ์๋น์ค์์๋ ์ผ๋ง๋์ ๋งํ๋์ง๋ฅผ ํด์ผ ํ๊ธฐ๋๋ฌธ์ timer๊ธฐ๋ฅ๋ ํ์ํ๋ฉฐ , ํ๊ณ ๋์ ๋์ ๋ฐ์ ์ ์ฌ๋์ ๋ํ ๊ฒฐ๊ณผ๋ ํ์ํ๋ค.
๋ค์์ผ๋ก๋ timer๋ฅผ ์ ์ฉํด๋ณผ ์์ ์ด๋ค.
'ionic' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๐ค ionic live-reload ํด๋ณด๊ธฐ (0) | 2022.10.22 |
|---|---|
| ๐ค ionic fcm background์์ ๋ฐ์ดํฐ ๋ฐ๊ธฐ (1) | 2022.03.20 |
| ๐ฅIonic์์ redux dev tools ์ฌ์ฉํด๋ณด๊ธฐ (0) | 2021.11.27 |
| ๐Ionic Android APK ์ถ์ถ (0) | 2021.10.01 |
| ๐ณโ๐Proxy ์ธํ (0) | 2021.09.07 |
๋๊ธ