dlutton / flutter_tts

Flutter Text to Speech package
MIT License
610 stars 258 forks source link

Text to speech not working on iOS in the background #368

Open noorbakerally opened 1 year ago

noorbakerally commented 1 year ago

πŸ› 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:

dlutton commented 1 year ago

@noorbakerally try using one of these categories:

iosAudioCategoryPlayback or iosAudioCategoryPlaybackAndRecord

noorbakerally commented 1 year ago

@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 commented 1 year ago

@dlutton any possible solution for this?

dlutton commented 1 year ago

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

noorbakerally commented 1 year ago

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

noorbakerally commented 1 year ago

@dlutton I've removed Flutterbeep but still no success

dlutton commented 1 year ago

@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.

noorbakerally commented 1 year ago

@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?

dlutton commented 1 year ago

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

nayanAubie commented 7 months ago

Same issue

Gaurav-CareMonitor commented 5 months ago

Facing Same Issue