The JavaScript API Speech Recognition enables web developers to incorporate speech recognition into your web page. This API allows fine control and flexibility over the speech recognition capabilities in Chrome version 25 and later. This API is experimental, that means that it's not available on every browser. Even in Chrome, there are some attributes of the API that aren't supported. For more information visit Can I Use Speech Recognition.
In this article you will learn how to use the Speech Recognition API, in its most basic expression.
Implementation
To get started, you will need to know wheter the browser supports the API or not. To do this, you can verify if the window object in the browser has the webkitSpeechRecognition property using any of the following snippets:
Once you verify, you can start to work with this API. Create a new instance of the webkitSpeechRecognition
class and set the basic properties:
Now that the basic options are set, you will need to add some event handlers. In this case we are going add the basic listeners as onerror
, onstart
, onend
and onresult
(event used to retrieve the recognized text).
onerror
onstart
onend
onresult
The onresult event receives as first parameter a custom even object. The results are stored in the event.results
property (an object of type SpeechRecognitionResultList
that stores the SpeechRecognitionResult
objects, this in turn contains instances of SpeechRecognitionAlternative
with the transcript property that contains the text).
As the final step, you need to start it by executing the start
method of the recognition object or to stop it once it's running executing the stop
method:
Now the entire functional snippet to use the speech recognition API should look like:
Once you execute the start method, the microphone permission dialog will be shown in the Browser.
Go ahead and test it in your web or local server. You can see a live demo of the Speech Recognition API working in the browser in all the available languages from the official Chrome Demos here.
Supported languages
Currently, the API supports 40 languages in Chrome. Some languages have specifical codes according to the region (the identifiers follow the BCP-47 format):
LANGUAGE | REGION | LANGUAGE CODE |
---|
Afrikaans | Default | af-ZA |
Bahasa Indonesia | Default | id-ID |
Bahasa Melayu | Default | ms-MY |
Català | Default | ca-ES |
Čeština | Default | cs-CZ |
Dansk | Default | da-DK |
Deutsch | Default | de-DE |
English | Australia | en-AU |
English | Canada | en-CA |
English | India | en-IN |
English | New Zealand | en-NZ |
English | South Africa | en-ZA |
English | United Kingdom | en-GB |
English | United States | en-US |
Español | Argentina | es-AR |
Español | Bolivia | es-BO |
Español | Chile | es-CL |
Español | Colombia | es-CO |
Español | Costa Rica | es-CR |
Español | Ecuador | es-EC |
Español | El Salvador | es-SV |
Español | España | es-ES |
Español | Estados Unidos | es-US |
Español | Guatemala | es-GT |
Español | Honduras | es-HN |
Español | México | es-MX |
Español | Nicaragua | es-NI |
Español | Panamá | es-PA |
Español | Paraguay | es-PY |
Español | Perú | es-PE |
Español | Puerto Rico | es-PR |
Español | República Dominicana | es-DO |
Español | Uruguay | es-UY |
Español | Venezuela | es-VE |
Euskara | Default | eu-ES |
Filipino | Default | fil-PH |
Français | Default | fr-FR |
Galego | Default | gl-ES |
Hrvatski | Default | hr_HR |
IsiZulu | Default | zu-ZA |
Íslenska | Default | is-IS |
Italiano | Italia | it-IT |
Italiano | Svizzera | it-CH |
Lietuvių | Default | lt-LT |
Magyar | Default | hu-HU |
Nederlands | Default | nl-NL |
Norsk bokmål | Default | nb-NO |
Polski | Default | pl-PL |
Português | Brasil | pt-BR |
Português | Portugal | pt-PT |
Română | Default | ro-RO |
Slovenščina | Default | sl-SI |
Slovenčina | Default | sk-SK |
Suomi | Default | fi-FI |
Svenska | Default | sv-SE |
Tiếng Việt | Default | vi-VN |
Türkçe | Default | tr-TR |
Ελληνικά | Default | el-GR |
български | Default | bg-BG |
Pусский | Default | ru-RU |
Српски | Default | sr-RS |
Українська | Default | uk-UA |
한국어 | Default | ko-KR |
中文 | 普通话 (中国大陆) | cmn-Hans-CN |
中文 | 普通话 (香港) | cmn-Hans-HK |
中文 | 中文 (台灣) | cmn-Hant-TW |
中文 | 粵語 (香港) | yue-Hant-HK |
日本語 | Default | ja-JP |
हिन्दी | Default | hi-IN |
ภาษาไทย | Default | th-TH |
You can use the following object if you need the previous table in Javascript and you can iterate it as shown in the example:
var allAvailableLanguages = [
{
"language": "Afrikaans",
"countryCodes": [
{
"langCode": "af-ZA",
"country": "Default"
}
]
},
{
"language": "Bahasa Indonesia",
"countryCodes": [
{
"langCode": "id-ID",
"country": "Default"
}
]
},
{
"language": "Bahasa Melayu",
"countryCodes": [
{
"langCode": "ms-MY",
"country": "Default"
}
]
},
{
"language": "Català",
"countryCodes": [
{
"langCode": "ca-ES",
"country": "Default"
}
]
},
{
"language": "Čeština",
"countryCodes": [
{
"langCode": "cs-CZ",
"country": "Default"
}
]
},
{
"language": "Dansk",
"countryCodes": [
{
"langCode": "da-DK",
"country": "Default"
}
]
},
{
"language": "Deutsch",
"countryCodes": [
{
"langCode": "de-DE",
"country": "Default"
}
]
},
{
"language": "English",
"countryCodes": [
{
"langCode": "en-AU",
"country": "Australia"
},
{
"langCode": "en-CA",
"country": "Canada"
},
{
"langCode": "en-IN",
"country": "India"
},
{
"langCode": "en-NZ",
"country": "New Zealand"
},
{
"langCode": "en-ZA",
"country": "South Africa"
},
{
"langCode": "en-GB",
"country": "United Kingdom"
},
{
"langCode": "en-US",
"country": "United States"
}
]
},
{
"language": "Español",
"countryCodes": [
{
"langCode": "es-AR",
"country": "Argentina"
},
{
"langCode": "es-BO",
"country": "Bolivia"
},
{
"langCode": "es-CL",
"country": "Chile"
},
{
"langCode": "es-CO",
"country": "Colombia"
},
{
"langCode": "es-CR",
"country": "Costa Rica"
},
{
"langCode": "es-EC",
"country": "Ecuador"
},
{
"langCode": "es-SV",
"country": "El Salvador"
},
{
"langCode": "es-ES",
"country": "España"
},
{
"langCode": "es-US",
"country": "Estados Unidos"
},
{
"langCode": "es-GT",
"country": "Guatemala"
},
{
"langCode": "es-HN",
"country": "Honduras"
},
{
"langCode": "es-MX",
"country": "México"
},
{
"langCode": "es-NI",
"country": "Nicaragua"
},
{
"langCode": "es-PA",
"country": "Panamá"
},
{
"langCode": "es-PY",
"country": "Paraguay"
},
{
"langCode": "es-PE",
"country": "Perú"
},
{
"langCode": "es-PR",
"country": "Puerto Rico"
},
{
"langCode": "es-DO",
"country": "República Dominicana"
},
{
"langCode": "es-UY",
"country": "Uruguay"
},
{
"langCode": "es-VE",
"country": "Venezuela"
}
]
},
{
"language": "Euskara",
"countryCodes": [
{
"langCode": "eu-ES",
"country": "Default"
}
]
},
{
"language": "Filipino",
"countryCodes": [
{
"langCode": "fil-PH",
"country": "Default"
}
]
},
{
"language": "Français",
"countryCodes": [
{
"langCode": "fr-FR",
"country": "Default"
}
]
},
{
"language": "Galego",
"countryCodes": [
{
"langCode": "gl-ES",
"country": "Default"
}
]
},
{
"language": "Hrvatski",
"countryCodes": [
{
"langCode": "hr_HR",
"country": "Default"
}
]
},
{
"language": "IsiZulu",
"countryCodes": [
{
"langCode": "zu-ZA",
"country": "Default"
}
]
},
{
"language": "Íslenska",
"countryCodes": [
{
"langCode": "is-IS",
"country": "Default"
}
]
},
{
"language": "Italiano",
"countryCodes": [
{
"langCode": "it-IT",
"country": "Italia"
},
{
"langCode": "it-CH",
"country": "Svizzera"
}
]
},
{
"language": "Lietuvių",
"countryCodes": [
{
"langCode": "lt-LT",
"country": "Default"
}
]
},
{
"language": "Magyar",
"countryCodes": [
{
"langCode": "hu-HU",
"country": "Default"
}
]
},
{
"language": "Nederlands",
"countryCodes": [
{
"langCode": "nl-NL",
"country": "Default"
}
]
},
{
"language": "Norsk bokmål",
"countryCodes": [
{
"langCode": "nb-NO",
"country": "Default"
}
]
},
{
"language": "Polski",
"countryCodes": [
{
"langCode": "pl-PL",
"country": "Default"
}
]
},
{
"language": "Português",
"countryCodes": [
{
"langCode": "pt-BR",
"country": "Brasil"
},
{
"langCode": "pt-PT",
"country": "Portugal"
}
]
},
{
"language": "Română",
"countryCodes": [
{
"langCode": "ro-RO",
"country": "Default"
}
]
},
{
"language": "Slovenščina",
"countryCodes": [
{
"langCode": "sl-SI",
"country": "Default"
}
]
},
{
"language": "Slovenčina",
"countryCodes": [
{
"langCode": "sk-SK",
"country": "Default"
}
]
},
{
"language": "Suomi",
"countryCodes": [
{
"langCode": "fi-FI",
"country": "Default"
}
]
},
{
"language": "Svenska",
"countryCodes": [
{
"langCode": "sv-SE",
"country": "Default"
}
]
},
{
"language": "Tiếng Việt",
"countryCodes": [
{
"langCode": "vi-VN",
"country": "Default"
}
]
},
{
"language": "Türkçe",
"countryCodes": [
{
"langCode": "tr-TR",
"country": "Default"
}
]
},
{
"language": "Ελληνικά",
"countryCodes": [
{
"langCode": "el-GR",
"country": "Default"
}
]
},
{
"language": "български",
"countryCodes": [
{
"langCode": "bg-BG",
"country": "Default"
}
]
},
{
"language": "Pусский",
"countryCodes": [
{
"langCode": "ru-RU",
"country": "Default"
}
]
},
{
"language": "Српски",
"countryCodes": [
{
"langCode": "sr-RS",
"country": "Default"
}
]
},
{
"language": "Українська",
"countryCodes": [
{
"langCode": "uk-UA",
"country": "Default"
}
]
},
{
"language": "한국어",
"countryCodes": [
{
"langCode": "ko-KR",
"country": "Default"
}
]
},
{
"language": "中文",
"countryCodes": [
{
"langCode": "cmn-Hans-CN",
"country": "普通话 (中国大陆)"
},
{
"langCode": "cmn-Hans-HK",
"country": "普通话 (香港)"
},
{
"langCode": "cmn-Hant-TW",
"country": "中文 (台灣)"
},
{
"langCode": "yue-Hant-HK",
"country": "粵語 (香港)"
}
]
},
{
"language": "日本語",
"countryCodes": [
{
"langCode": "ja-JP",
"country": "Default"
}
]
},
{
"language": "हिन्दी",
"countryCodes": [
{
"langCode": "hi-IN",
"country": "Default"
}
]
},
{
"language": "ภาษาไทย",
"countryCodes": [
{
"langCode": "th-TH",
"country": "Default"
}
]
}
];
allAvailableLanguages.forEach(function(item){
if(item.countryCodes.length > 1){
var allCountries = "";
console.log(item.language + " ("+item.countryCodes.length+" country(s)) ");
item.countryCodes.forEach(function(countryItem){
console.log(" "+countryItem.country + " | "+ countryItem.langCode);
});
}else{
console.log(item.language + " ["+item.countryCodes[0].langCode+"]");
}
});
Whose output in the console will be:
Happy coding !
Comments
Post a Comment