flutter_tts icon indicating copy to clipboard operation
flutter_tts copied to clipboard

Text to speech not working on iOS in the background

Open noorbakerally opened this issue 2 years ago • 11 comments

🐛 Bug Report

I am unable to play an audio that is generated using text to speech plugin on iOS only.

Expected behavior

I am getting the following error:
2023-01-11 09:52:04.095673+0400 Runner[12016:2350386] [AXTTSCommon] Failure starting audio queue alp!
2023-01-11 09:52:05.218595+0400 Runner[12016:2350386] [AXTTSCommon] _BeginSpeaking: couldn't begin playback

Background Settings in info.plist

<key>UIBackgroundModes</key>
    <array>
        <string>audio</string>
        <string>fetch</string>
        <string>location</string>
        <string>processing</string>
    </array>

Speaker Configuration

Future<void> initializeSpeaker() async {
    speakerPlugin = FlutterTts();
    speakerPlugin.setQueueMode(0);

    /////////////////////////////
    //Selecting voices
    ////////////////////////////
    var voices = await speakerPlugin.getVoices;

    //example {name: ur-PK-language, locale: ur-PK}



    String deviceLanguage= Platform.localeName.substring(0,2);
    int defaultvoiceix = 0;
    String userlocale = "en-gb";

    try {
      String languageCode = Platform.localeName.split('_')[0];
      String countryCode = Platform.localeName.split('_')[1];
      userlocale = languageCode + "-"+countryCode;
    } catch (exception) {
      userlocale = "en-gb";
    }

    for (var i=0;i<voices.length;i++) {
      String currentLocale = voices[i]["locale"];
      if (currentLocale.toLowerCase() == userlocale.toLowerCase()){
        defaultvoiceix = i;
      }
    }
    
    await speakerPlugin.setSharedInstance(true);
    await speakerPlugin.setVoice(voices[defaultvoiceix]);
    await speakerPlugin.setVolume(1.0);
    await speakerPlugin.setSpeechRate(1.0);
    await speakerPlugin.setPitch(1.0);
    await speakerPlugin.setLanguage(userlocale);
    await speakerPlugin
        .setIosAudioCategory(IosTextToSpeechAudioCategory.playback, [
      IosTextToSpeechAudioCategoryOptions.mixWithOthers,
      IosTextToSpeechAudioCategoryOptions.duckOthers
    ]);
  }

Reproduction steps

Configuration

Version: 0.1.x

Platform:

  • iphone: iOS

noorbakerally avatar Jan 11 '23 06:01 noorbakerally

@noorbakerally try using one of these categories:

iosAudioCategoryPlayback or iosAudioCategoryPlaybackAndRecord

dlutton avatar Jan 11 '23 18:01 dlutton

@dlutton I am already using it, As it can be seen in the code i above, i am getting the category playback as below

await speakerPlugin
        .setIosAudioCategory(IosTextToSpeechAudioCategory.playback, [
      IosTextToSpeechAudioCategoryOptions.mixWithOthers,
      IosTextToSpeechAudioCategoryOptions.duckOthers
    ]);

I have tried playbackAndRecord also, but no success

noorbakerally avatar Jan 11 '23 23:01 noorbakerally

@dlutton any possible solution for this?

noorbakerally avatar Jan 17 '23 06:01 noorbakerally

@noorbakerally I've yet to recreate the issue, are you running any other voice packages that could be interfering with it?

dlutton avatar Jan 17 '23 07:01 dlutton

@dlutton I use Flutterbeep to make beep sounds, i'll check whether removing the package make any difference.

noorbakerally avatar Jan 17 '23 13:01 noorbakerally

@dlutton I've removed Flutterbeep but still no success

noorbakerally avatar Jan 18 '23 12:01 noorbakerally

@noorbakerally I'll continue looking into this issue. Usually it's an issue with not setting the right audio category, but it looks like you're setting it.

dlutton avatar Jan 18 '23 18:01 dlutton

@dlutton we tried something on our side, we removed the line await speakerPlugin.setVoice(voices[defaultvoiceix]); and it's now working even in the background. However, this line is important for us to set a language based on the user's regional settings. Any idea why this line could be causing this?

noorbakerally avatar Jan 25 '23 04:01 noorbakerally

@noorbakerally can you provide the value you're passing into setVoice? I'll attempt to recreate the issue locally.

dlutton avatar Jan 25 '23 17:01 dlutton

Same issue

nayanAubie avatar Mar 18 '24 11:03 nayanAubie

Facing Same Issue

Gaurav-CareMonitor avatar May 14 '24 08:05 Gaurav-CareMonitor