flutter_tts icon indicating copy to clipboard operation
flutter_tts copied to clipboard

setProgressHandler callback is not working with finnish language

Open echogit opened this issue 9 months ago • 2 comments

I use the call back _flutterTts.setProgressHandler((String text, int startOffset, int endOffset, String word) to be notified of each word that is being spoken by the TTS. It works alright for android in all the 20 languages I have on my app (Speak Out Kids on Google Play). On iOS it also works with all languages, except Finnish. On Finnish instead of starting on 0 it starts on 15, which sort of breaks the functionality.

I created a reproduceable test on dart (full code further below), you just need to drop it inside a project that has the flutter_tts dependency. The result, check the indexes

Image

Reproduction steps

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter TTS Demo',
      home: TtsDemoPage(),
    );
  }
}

class TtsDemoPage extends StatefulWidget {
  @override
  _TtsDemoPageState createState() => _TtsDemoPageState();
}

class _TtsDemoPageState extends State<TtsDemoPage> {
  final FlutterTts _flutterTts = FlutterTts();
  String _callbackOutput = "";
  String _language = "";

  @override
  void initState() {
    super.initState();
    // Configura o progress handler para capturar os offsets e a palavra atual
    _flutterTts.setProgressHandler((String text, int startOffset, int endOffset, String word) {
      String output = "tts text - startOffset: $startOffset - endOffset: $endOffset - Word: $word\n";
      debugPrint(output);
      setState(() {
        _callbackOutput += output;
      });
    });
  }

  Future<void> _speakEnglish() async {
    _language = "en-US";
    String textToSPeak = "Hello, this is an English test.";
    await _flutterTts.setLanguage("en-US");
    
    setState(() {
      _callbackOutput = _callbackOutput + "\n\n========== Speaking in English ==========\n";
      _callbackOutput = _callbackOutput + "\n$textToSPeak\n";
    });
    await _flutterTts.speak(textToSPeak);
  }

  Future<void> _speakFinnish() async {
    _language = "fi-FI";
    String textToSPeak = "Hei, tämä on suomen testaa.";
    await _flutterTts.setLanguage("fi-FI");
    
    setState(() {
      _callbackOutput = _callbackOutput + "\n\n========== Speaking in Finnish ==========\n";
      _callbackOutput = _callbackOutput + "\n$textToSPeak\n";
    });
    await _flutterTts.speak(textToSPeak);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter TTS Demo"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            ElevatedButton(
              onPressed: _speakEnglish,
              child: Text("Speak in English"),
            ),
            SizedBox(height: 10),
            ElevatedButton(
              onPressed: _speakFinnish,
              child: Text("Speak in Finnish"),
            ),
            SizedBox(height: 20),
            Expanded(
              child: SingleChildScrollView(
                child: Text(
                  _callbackOutput,
                  style: TextStyle(fontFamily: 'monospace'),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

Configuration

Finish language: fi-FI

Version: 4.2.2

Platform:

  • [x] :iphone: iOS

echogit avatar Feb 24 '25 04:02 echogit

I tried to add this:

      if (appSettingsProvider.language.startsWith('fi') && isIOS) {
        startOffset=startOffset-15;
        endOffset=endOffset-15;
      }

But still, it didn't helped much, as depending on the text it varies the point where it start the initial index, sometimes 15, sometimes 16.

echogit avatar Feb 24 '25 14:02 echogit

Would this be a problem specifically with iOS TTS native implementation?

echogit avatar Mar 08 '25 19:03 echogit