Search code examples
xamarin.iosxamarin.formstext-to-speech

Xamarin Forms Text To Speech Wont Play On Device Without Headphones


I have a Xamarin Forms app that uses text to speech. The code in the iOS project is exactly the same as on the official Xamarin Implementing Text To Speech guide

    var speechSynthesizer = new AVSpeechSynthesizer ();
    var speechUtterance = new AVSpeechUtterance (text) {
        Rate = AVSpeechUtterance.MaximumSpeechRate/4,
        Voice = AVSpeechSynthesisVoice.FromLanguage ("en-US"),
        Volume = 0.5f,
        PitchMultiplier = 1.0f
    };

    speechSynthesizer.SpeakUtterance (speechUtterance);

This works great in the iOS emulator but when I deploy to my iPad running iOS 10.2.1 I only hear any sound if I put headphones on. I've turned the volume right up and still nothing without headphones, if I do use headphones though it's very loud so implies it's not just too quiet. Other apps play audio fine through my iPad speaker so I don't think it's a hardware issue.

I've also tried using this plugin (https://github.com/jamesmontemagno/TextToSpeechPlugin) for text to speech but it has the exact same issue when I deploy to the device.

Is there any reason this would work with headphones but fail when no headphones are plugged in?


Solution

  • I see no apparent reason why your code should not work. The only explanation I have is that 'silent mode' is activated on your iDevice. To switch this off, either use the switch on the left side, or for newer devices to into the control center by swiping up from the bottom of the screen and pressing the bell icon.

    iOS control center

    You also note that other apps do produce sound. As a developer, you may choose to ignore the silent mode and play sound anyway, while this should be considered bad practice, it is allowed by Apple. However, it is not enabled by default, so with the code you have sound will be muted by silent mode.