--- title: SpeechRecognition slug: Web/API/SpeechRecognition tags: - API - Experimental - Interface - Reference - SpeechRecognition - Web Speech API - recognition - speech translation_of: Web/API/SpeechRecognition ---
{{APIRef("Web Speech API")}}{{SeeCompatTable}}
Web Speech API の SpeechRecognition インターフェイスは、認識サービスの制御インターフェイスです。これは、認識サービスから送信された {{domxref("SpeechRecognitionEvent")}} も扱います。
注記: Chrome では、ウェブページ上で音声認識を使用するとサーバーベースの認識エンジンが使用されます。あなたの音声を認識処理するためにウェブサービスへ送信するのでオフラインでは動作しません。
SpeechRecognition オブジェクトを作成します。SpeechRecognition は、親インターフェイスである {{domxref("EventTarget")}} からのプロパティも継承します。
SpeechRecognition により理解される文法を表す、{{domxref("SpeechGrammar")}} オブジェクトのコレクションを返して設定します。SpeechRecognition の言語を返して設定します。指定されない場合、これはデフォルトで HTML {{htmlattrxref("lang","html")}} 属性の値になります。どちらも設定されていない場合、ユーザーエージェントの言語設定が使用されます。false) です。true)、そうでないか (false) を制御します。暫定的な結果は、最終的な結果ではありません (つまり、{{domxref("SpeechRecognitionResult.isFinal")}} プロパティの値は false)。SpeechRecognition に使用される音声認識サービスの場所を指定します。デフォルトはユーザーエージェントのスピーチサービスです。SpeechRecognition に関連付けられた文法の認識が意図された入力音声のリスニングを開始した時に発火します。SpeechRecognition は、その親インターフェイスである {{domxref("EventTarget")}} からのメソッドも継承します。
SpeechRecognition に関連付けられた文法の認識を行います。私たちのシンプルな Speech color changer の例では、{{domxref("SpeechRecognition.SpeechRecognition", "SpeechRecognition()")}} コンストラクターを使用して新しい SpeechRecognition オブジェクトのインスタンスを生成し、新しい {{domxref("SpeechGrammarList")}} を作成、それを {{domxref("SpeechRecognition.grammars")}} プロパティを使用して SpeechRecognition インスタンスにより認識される文法に設定します。
他の値を定義した後、私たちは、それを設定して、クリックイベントの発生時 ({{domxref("SpeechRecognition.start()")}} 参照) に認識サービスを開始します。音声の認識に成功すると、{{domxref("SpeechRecognition.onresult")}} ハンドラーが発火し、イベントオブジェクトから発話された色を展開、そしてそれを {{htmlelement("html")}} 要素の背景色に設定します。
var grammar = '#JSGF V1.0; grammar colors; public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghostwhite | gold | goldenrod | gray | green | indigo | ivory | khaki | lavender | lime | linen | magenta | maroon | moccasin | navy | olive | orange | orchid | peru | pink | plum | purple | red | salmon | sienna | silver | snow | tan | teal | thistle | tomato | turquoise | violet | white | yellow ;'
var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
//recognition.continuous = false;
recognition.lang = 'en-US';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
var diagnostic = document.querySelector('.output');
var bg = document.querySelector('html');
document.body.onclick = function() {
recognition.start();
console.log('Ready to receive a color command.');
}
recognition.onresult = function(event) {
var color = event.results[0][0].transcript;
diagnostic.textContent = 'Result received: ' + color;
bg.style.backgroundColor = color;
}
| 仕様書 | 策定状況 | 備考 |
|---|---|---|
| {{SpecName('Web Speech API', '#speechreco-section', 'SpeechRecognition')}} | {{Spec2('Web Speech API')}} |
{{Compat("api.SpeechRecognition")}}
アプリ内で音声認識を使用するには、以下の許可設定を manifest で指定する必要があります:
"permissions": {
"audio-capture" : {
"description" : "Audio capture"
},
"speech-recognition" : {
"description" : "Speech recognition"
}
}
特権アプリも必要なため、以下も同様に含める必要があります:
"type": "privileged"