OLD | NEW |
(Empty) | |
| 1 <div id="pageData-name" class="pageData">Speech Input API</div> |
| 2 |
| 3 <!-- BEGIN AUTHORED CONTENT --> |
| 4 <p id="classSummary"> |
| 5 The <code>chrome.experimental.speechInput</code> module provides |
| 6 one-shot speech recognition to Chrome extensions. |
| 7 This module is still experimental. For information on how to use experimental |
| 8 APIs, see the <a href="experimental.html">chrome.experimental.* APIs</a> page. |
| 9 </p> |
| 10 |
| 11 <h2 id="manifest">Manifest</h2> |
| 12 <p>You must declare the "experimental" permission in the <a |
| 13 href="manifest.html">extension manifest</a> to use the speech input |
| 14 API. |
| 15 For example:</p> |
| 16 <pre>{ |
| 17 "name": "My extension", |
| 18 ... |
| 19 <b>"permissions": [ |
| 20 "experimental" |
| 21 ]</b>, |
| 22 ... |
| 23 }</pre> |
| 24 |
| 25 <h2 id="howToStart">How to start speech recognition</h2> |
| 26 <p>To start recognizing speech an extension must call the <code>start()</code> |
| 27 method. If provided, your callback will be called once recording has |
| 28 successfully started. In case of error <code>chrome.extension.lastError</code> |
| 29 will be set.</p> |
| 30 |
| 31 <p>This API provides exclusive access to the default recording device to the |
| 32 first extension requesting it. Consequently, any calls to <code>start()</code> |
| 33 when the device is being used by another extension or web page will fail and set |
| 34 <code>chrome.extension.lastError</code>. The message <code>requestDenied</code> |
| 35 will be set if another extension in the same profile is making use of the API. |
| 36 Otherwise <code>noRecordingDeviceFound</code>, <code>recordingDeviceInUse</code> |
| 37 or <code>unableToStart</code> will be set depending on the situation.</p> |
| 38 |
| 39 <p>To check whether recording is currently active, call the |
| 40 <code>isRecording()</code> method. Please note that it only checks for audio |
| 41 recording within Chrome.</p> |
| 42 |
| 43 |
| 44 <h2 id="howToGetResults">How to get speech recognition results</h2> |
| 45 <p>Listen to the <code>onResult</code> event to receive speech recognition |
| 46 results.</p> |
| 47 |
| 48 <pre> |
| 49 var callback = function(result) { ... }; |
| 50 |
| 51 chrome.experimental.speechInput.onResult.addListener(callback); |
| 52 </pre> |
| 53 |
| 54 <p>The <code>result</code> object contains an array of |
| 55 <a href="#type-SpeechInputResultHypothesis">SpeechInputResultHypothesis</a> |
| 56 sorted by decreasing likelihood.</p> |
| 57 |
| 58 <p>Recording automatically stops when results are received. It is safe to call |
| 59 <code>start()</code> again from the results callback.</p> |
| 60 |
| 61 <p>To handle errors during speech recognition listen for the |
| 62 <code>onError</code> event.</p> |
| 63 |
| 64 <pre> |
| 65 var callback = function(error) { ... }; |
| 66 |
| 67 chrome.experimental.speechInput.onError.addListener(callback); |
| 68 </pre> |
| 69 |
| 70 </p>Recording will automatically stop in case of error. |
| 71 It is safe to call <code>start()</code> again from the error callback.</p> |
| 72 |
| 73 |
| 74 <h2 id="howToStop">How to stop recording</h2> |
| 75 <p>To stop speech recognition call the <code>stop()</code> method. If provided, |
| 76 the callback function will be called once recording has successfully stopped. |
| 77 In case of error <code>chrome.extension.lastError</code> will be set. |
| 78 </p> |
| 79 |
| 80 |
| 81 <h2 id="otherFeatures">Other features</h2> |
| 82 <ul><li> |
| 83 <code>onSoundStart</code> - Event generated when start of sound is detected |
| 84 (from previously being silent). |
| 85 </li><li> |
| 86 <code>onSoundEnd</code> - Event generated when end of sound is detected (a |
| 87 continued period of silence). |
| 88 </li></ul> |
| 89 |
| 90 |
| 91 <h2 id="examples">Examples</h2> |
| 92 <p>The following example illustrates how to show a JavaScript alert with the |
| 93 most likely recognition result.</p> |
| 94 <pre> |
| 95 function checkStart() { |
| 96 if (chrome.extension.lastError) { |
| 97 alert("Couldn't start speech input: " + chrome.extension.lastError.message); |
| 98 } |
| 99 } |
| 100 |
| 101 function recognitionFailed(error) { |
| 102 alert("Speech input failed: " + error.code); |
| 103 } |
| 104 |
| 105 function recognitionSucceeded(result) { |
| 106 alert("Recognized '" + result.hypotheses[0].utterance + "' with confidence " +
result.hypotheses[0].confidence); |
| 107 } |
| 108 |
| 109 chrome.experimental.speechInput.onError.addListener(recognitionFailed); |
| 110 chrome.experimental.speechInput.onResult.addListener(recognitionSucceded); |
| 111 chrome.experimental.speechInput.start({ "language": "en" }, checkStart); |
| 112 </pre> |
OLD | NEW |