Hi,
I have an Xamarin.Form iOS app that has a button that speaks some text. I'm trying to check if the app is currently speaking by checking the AVSpeechSynthesizer.Speaking property, but it always returning false.
What I want the app to do is if it's currently speaking and the user clicks on the "Speak" button it should stop speaking, otherwise start speaking.
Below is my code. Thanks.
public void Speak(string text)
{
_speechSynthesizer = new AVSpeechSynthesizer();
// adjust the speech rate for ios 8
var speechRate = UIDevice.CurrentDevice.CheckSystemVersion(8, 0) ? 8 : 4;
var speechUtterance = new AVSpeechUtterance(text)
{
Rate = AVSpeechUtterance.MaximumSpeechRate / speechRate,
Voice = AVSpeechSynthesisVoice.FromLanguage("en-US"),
Volume = 0.5f,
PitchMultiplier = 1.0f
};
if (_speechSynthesizer.Speaking)
{
_speechSynthesizer.StopSpeaking(AVSpeechBoundary.Immediate);
}
else
{
Debug.WriteLine("Start speaking");
_speechSynthesizer.SpeakUtterance(speechUtterance);
}
Debug.WriteLine("Speaking Value: {0}", _speechSynthesizer.Speaking);
}