Text To Speech in Xamarin.Forms and .NET MAUI with Essentials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to look at text to speech which is part of the xamarin essentials apis and to be honest this video came to be because i read a comment wrong in that comment someone asked me hey can you show me how to do speech to text but you know i read over it really quickly and i thought they were saying text to speech so here's a video on text speech and i think in the near future you can also expect one on speech to text but let's first look at this one so what you see here is a file new xamarin forms application on the left you can see the example page and on the right you can see it running on the ios simulator so this is basically the default template that comes out of the box whenever you run this now on visual studio 2019 or visual studio 2000 need for mac so here it is and by the power of hot reload i can just start editing here so i can hear say text to speech sample save that and boom it will update automatically so that is very cool now i don't need all the rest of this ui so let's make this a little bit more useful to whatever we are doing and for our case i'm going to implement a little picker here you will see why in a little bit and give that a name and here we are going to show all the language languages there we go and underneath that we're going to have an entry which will take the text that we actually want to you know speak so um text to speak talking about a nice name and we're going to have a button to actually trigger that speaking action so here we go text is speak please you know always be polite and we're going to have a clicked handler for that to create a new one and there we go so now our ui is basically ready only one thing it seems to be wrong i don't know what that is let's try that again maybe the intellisense is just having a weird day okay i think this is okay so here you can see the new ui showing up on the right we have a picker which does nothing at the moment here we have an entry where we can enter some text and this is the speak please where we can do our actual speak action so we've implemented this that's pretty cool now let's go to our main page code behind i have it open here already and you can see the button clicked handler is here and of course like the other things that you would expect from an empty page so the first thing i'm going to add here at the top is the using xamarin essentials so you know that makes it a little bit more readable and so if we're going to explore this api we're going to go to text to speech there we go and it has a couple of things so it has get locales async and that gets all the locales that are supported by your device um to use text to speech on so this will give you a list with like you know for instance english dutch french i don't know all the things that are supported on your device and this is one of the things that we need to specify um for the actual text-to-speech so it's it knows which language to um put out there and it will sound a little bit better because you know if you're going to enter dutch text in an english voice then that's not going to sound right so this is the the list that we're going to show in our picker and then we're going to get the actual chosen option from the user and we're going to use that for speaking the actual text and the other api we are looking at here is the speak async so that does the actual speak action so that's going to utter our text in the locale that we have selected now there's of course one problem because this does async so we can either solve this not so nice in the constructor or you know another workaround if you have to deal with this stuff is to override the on appearing and here you can say i want to have this as async and now we can do all the things so actually let us introduce a new variable here at the top level which is going to be an i enumerable of that locale that we've been looking at let's call it locales that's fine and here we're going to say locales is text to speech dot get locales async so there we go and of course we need to await this that's why we were doing this so now on each on appearing of this screen so note that might not be very efficient if you are going back and forth between different screens but hopefully you're using some kind of mvvm pattern or framework where you could do this another way or you could you know just see if there's something in your locales collection already and then skip this but you know this is just to show you another way to overcome like this async await pattern that you cannot use in the constructor here um so now we got our locales so that is great and then on our button clicked we are going to see like oh wait we need to do something extra because now it's just in our variable we actually need to um you know put this in our picker so for each for l in locales we're going to do this in the languages dot items items dot add and we're going to use l dot so this has a couple of things the country the id the language the name so let's go for the name i'm going to assume that they are unique so we're going to put all the names in there of the languages and if you're looking for like the the iso formatted kind of things and i think you need the languages so that's going to be like the e n u s or n l dash n l that's kind of things but i think the name is probably going to be like the human readable form of this locale so let's see what that brings us then we're going to go over to our button and we're going to look first if our languages has a selected index of more than zero because else probably things will crash so let's see if the user actually selected a language to speak and from there we're just going to say text to speech dot speak and here you see a couple of things so we're going to need the text which is going to be our text to speak nice naming here the text there we go and um this should be enough and this is going to be using the default option so i'm going to assume that this does the english voice but you know so we have the picker in in place already so let's just use that and we have one over right here that takes speech options so let's create a new speech options and this has a couple of properties it takes that locale that we selected it takes the pitch and it takes the volume so you can also play a little with the pitch and the volume that's kind of funny so you can make the voice sound a little bit higher or the volume can be you know louder or softer but i'm just going to use the locale for now and actually what we need to do here then is do that locales dot single so i'm going to skip over a little bit of error checking here and this is a little bit of link magic and here we are selecting where name is going whoops name is going to be the languages dot selected item dot to string so this might seem a little bit of magic if you've never worked with this before also link link is written l i and q has a couple of extension methods that you can mostly apply on like collections and other things as well but on collections and here we say like we're going to select a single from from this a single value from this collection and um to you know select that actual single value we're going to supply this lambda here and this is going to be you know the value going in so this is basically it will skip over all of the items in this collection and you can reach those with this l and whenever the l so the locale that we are looking at currently dot name equals the name that we selected in our picker so it has a selected item that is just the item that we added here to our picker so that's just going to be the the name string here that's why i call the tostring because you know it can be any object basically but now i know it's just a string and we're going to see whenever that is true then it's going to pick that locale for us and assign it to the locale property of this speech option so i hope that makes a little bit sense and actually that is all we need to do so i need to stop and restart the application now real quickly because you know we made changes in the code and that is not um he had reloaded yet maybe that's yet to come who knows what might happen but here we go the app should come up again and now everything should be in place so if we go into this picker then we see all the languages here it seems i was incorrect so this is still all the iso kind of you know values of all the locales that are in there and if we're going to go to like you know my native tongue so nlnl done and we're going to say hello alamal who which means hello everyone how's it going [Music] i see there's some some auto suggest going on here because the the simulator is probably set to english so it now says hello all email how is it going um well anyway so let's let's go to english and see if we can can do a little bit with that and we're going to remove this and we're going to you know do something completely random like uh i don't know what should it be have you subscribed to gerald's channel yet you should and speak have you subscribed to gerald's channel yet you should so here you go you can see this just works and um so it just to show you what happens if you do the wrong locale with the wrong kind of like language in in the text so we're going to do spanish now and you can see that um well let's see you can hear that it sounds kind of funny right so you should probably make sure that um yeah they entered the right kind of language so that is all there is to it to make text-to-speech work on your devices i'm not sure actually if this will work on the android emulator i think i tested it on an emulator and it didn't really work it does at least work on the ios simulator so if you want to be really really sure just hook it up to your physical device and you know it will work on that either way i keep being amazed by the simplicity of xamarin essentials with just two apis they've enabled us to write text-to-speech functionality in just a few lines of code that is so amazing so this was it for another examine essentials api kind of review walkthrough however you want to call it i hope you enjoyed this one if you did please like this video let me know in the comments just like you know how we started this video what topic you would like to see next if you would like to have a little bit more priority with what topic you want to see please consider joining my channel as a member where you can do just that for a small fee and i will be seeing you for my next [Music] video
Info
Channel: Gerald Versluis
Views: 3,482
Rating: undefined out of 5
Keywords: xamarin, xamarin.forms, Xamarin.Essentials, Xamarin Essentials, xamarin essentials text to speech, Xamarin.Essentials text to speech, xamarin text to speech, xamarin forms text to speech, text to speech xamarin forms, Xamarin.Forms 101, Xamarin Forms 101, xamarin forms tutorial, xamarin.forms tutorial, Xamarin essentials tutorial, app speak, Let your app speak, TTS, Xamarin TTS, Xamarin.Forms TTS, Xamarin Forms TTS, ios text to speech, xamarin tutorial, xamarin forms, TextToSpeech
Id: yw5veY4dIrk
Channel Id: undefined
Length: 12min 23sec (743 seconds)
Published: Mon Nov 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.