tts_plugin 1.1.0 copy "tts_plugin: ^1.1.0" to clipboard
tts_plugin: ^1.1.0 copied to clipboard

A flutter plugin for Text to Speech. This plugin is supported on iOS, Android, Web, & macOS.

example/lib/main.dart

import 'dart:async';
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;

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

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

enum TtsState { playing, stopped, paused, continued }

class _MyAppState extends State<MyApp> {
  FlutterTts flutterTts;
  double volume = 0.5;
  double pitch = 1.0;
  double rate = 0.5;

  String _newVoiceText = "哈哈哈测试TTS语音转文字-very good";

  TtsState ttsState = TtsState.stopped;

  get isPlaying => ttsState == TtsState.playing;

  get isStopped => ttsState == TtsState.stopped;

  get isPaused => ttsState == TtsState.paused;

  get isContinued => ttsState == TtsState.continued;

  List<String> engines;
  List<String> languages;
  List<String> voices;

  String engine;
  String lang;
  String voice;

  @override
  initState() {
    super.initState();
    initTts();
  }

  initTts() async {
    flutterTts = FlutterTts();
    var enable = await flutterTts.enable;
    if (!enable) {
      print("flutterTts.enable = false");
    } else {
      if (!kIsWeb) {
        if (Platform.isAndroid) {
          _getEngines();
        }
      }

      flutterTts.setStartHandler(() {
        setState(() {
          print("Playing");
          ttsState = TtsState.playing;
        });
      });

      flutterTts.setCompletionHandler(() {
        setState(() {
          print("Complete");
          ttsState = TtsState.stopped;
        });
      });

      flutterTts.setCancelHandler(() {
        setState(() {
          print("Cancel");
          ttsState = TtsState.stopped;
        });
      });

      if (kIsWeb || Platform.isIOS) {
        flutterTts.setPauseHandler(() {
          setState(() {
            print("Paused");
            ttsState = TtsState.paused;
          });
        });

        flutterTts.setContinueHandler(() {
          setState(() {
            print("Continued");
            ttsState = TtsState.continued;
          });
        });
      }

      flutterTts.setErrorHandler((msg) {
        setState(() {
          print("error: $msg");
          ttsState = TtsState.stopped;
        });
      });

      engines = await flutterTts.getEngines;
      languages = (await flutterTts.getLanguages).where((e) => e.startsWith("zh")).toList();
      voices = await flutterTts.getVoices;
      setState(() {});
    }
  }

  Future _getEngines() async {
    var engines = await flutterTts.getEngines;
    if (engines != null) {
      for (dynamic engine in engines) {
        print(engine);
      }
    }
  }

  Future _speak() async {
    await flutterTts.setVolume(volume);
    await flutterTts.setSpeechRate(rate);
    await flutterTts.setPitch(pitch);

    if (_newVoiceText != null) {
      if (_newVoiceText.isNotEmpty) {
        await flutterTts.awaitSpeakCompletion(true);
        await flutterTts.speak(_newVoiceText);
      }
    }
  }

  Future _stop() async {
    var result = await flutterTts.stop();
    if (result == 1) setState(() => ttsState = TtsState.stopped);
  }

  Future _pause() async {
    var result = await flutterTts.pause();
    if (result == 1) setState(() => ttsState = TtsState.paused);
  }

  @override
  void dispose() {
    super.dispose();
    flutterTts.stop();
  }

  List<DropdownMenuItem<String>> getEngineDropDownMenuItems() {
    var items = List<DropdownMenuItem<String>>();
    for (String type in engines) {
      items.add(DropdownMenuItem(value: type as String, child: Text(type as String)));
    }
    return items;
  }

  Future<void> changedEngineDropDownItem(String selectedType) async {
    var flag = await flutterTts.setEngine(selectedType);
    print("setEngine: $flag");

    languages = (await flutterTts.getLanguages).where((e) => e.startsWith("zh")).toList();
    voices = await flutterTts.getVoices;

    setState(() {
      engine = selectedType;
    });
  }

  List<DropdownMenuItem<String>> getLanguageDropDownMenuItems() {
    var items = List<DropdownMenuItem<String>>();
    for (String type in languages) {
      items.add(DropdownMenuItem(value: type as String, child: Text(type as String)));
    }
    return items;
  }

  Future<void> changedLanguageDropDownItem(String selectedType) async {
    var flag = await flutterTts.setLanguage(selectedType);
    print("setLanguage: $flag");
  }

  List<DropdownMenuItem<String>> getVoiceDropDownMenuItems() {
    var items = List<DropdownMenuItem<String>>();
    for (String type in voices) {
      items.add(DropdownMenuItem(value: type as String, child: Text(type as String)));
    }
    return items;
  }

  Future<void> changedVoiceDropDownItem(String selectedType) async {
    var flag = await flutterTts.setVoice(selectedType);
    print("setVoice: $flag");
  }

  void _onChange(String text) {
    setState(() {
      _newVoiceText = text;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text('Flutter TTS'),
            ),
            body: SingleChildScrollView(
                scrollDirection: Axis.vertical,
                child: Column(children: [
                  _inputSection(),
                  _btnSection(),
                  engines != null ? _engineDropDownSection() : Text(""),
                  languages != null ? _languageDropDownSection() : Text(""),
                  engines != null ? _voiceDropDownSection() : Text(""),
                  _buildSliders()
                ]))));
  }

  Widget _inputSection() => Container(
      alignment: Alignment.topCenter,
      padding: EdgeInsets.only(top: 25.0, left: 25.0, right: 25.0),
      child: TextField(
        onChanged: (String value) {
          _onChange(value);
        },
      ));

  Widget _btnSection() {
    if (!kIsWeb && Platform.isAndroid) {
      return Container(
          padding: EdgeInsets.only(top: 50.0),
          child: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
            _buildButtonColumn(Colors.green, Colors.greenAccent, Icons.play_arrow, 'PLAY', _speak),
            _buildButtonColumn(Colors.red, Colors.redAccent, Icons.stop, 'STOP', _stop),
          ]));
    } else {
      return Container(
          padding: EdgeInsets.only(top: 50.0),
          child: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
            _buildButtonColumn(Colors.green, Colors.greenAccent, Icons.play_arrow, 'PLAY', _speak),
            _buildButtonColumn(Colors.red, Colors.redAccent, Icons.stop, 'STOP', _stop),
            _buildButtonColumn(Colors.blue, Colors.blueAccent, Icons.pause, 'PAUSE', _pause),
          ]));
    }
  }

  Widget _engineDropDownSection() => Container(
      padding: EdgeInsets.only(top: 50.0),
      child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
        DropdownButton(
          value: engine,
          items: getEngineDropDownMenuItems(),
          onChanged: changedEngineDropDownItem,
        )
      ]));

  Widget _languageDropDownSection() => Container(
      padding: EdgeInsets.only(top: 50.0),
      child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
        DropdownButton(
          value: lang,
          items: getLanguageDropDownMenuItems(),
          onChanged: changedLanguageDropDownItem,
        )
      ]));

  Widget _voiceDropDownSection() => Container(
      padding: EdgeInsets.only(top: 50.0),
      child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
        DropdownButton(
          value: voice,
          items: getVoiceDropDownMenuItems(),
          onChanged: changedVoiceDropDownItem,
        )
      ]));

  Column _buildButtonColumn(Color color, Color splashColor, IconData icon, String label, Function func) {
    return Column(mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [
      IconButton(icon: Icon(icon), color: color, splashColor: splashColor, onPressed: () => func()),
      Container(
          margin: const EdgeInsets.only(top: 8.0),
          child: Text(label, style: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w400, color: color)))
    ]);
  }

  Widget _buildSliders() {
    return Column(
      children: [_volume(), _pitch(), _rate()],
    );
  }

  Widget _volume() {
    return Slider(
        value: volume,
        onChanged: (newVolume) {
          setState(() => volume = newVolume);
        },
        min: 0.0,
        max: 1.0,
        divisions: 10,
        label: "Volume: $volume");
  }

  Widget _pitch() {
    return Slider(
      value: pitch,
      onChanged: (newPitch) {
        setState(() => pitch = newPitch);
      },
      min: 0.5,
      max: 2.0,
      divisions: 15,
      label: "Pitch: $pitch",
      activeColor: Colors.red,
    );
  }

  Widget _rate() {
    return Slider(
      value: rate,
      onChanged: (newRate) {
        setState(() => rate = newRate);
      },
      min: 0.0,
      max: 1.0,
      divisions: 10,
      label: "Rate: $rate",
      activeColor: Colors.green,
    );
  }
}
0
likes
40
pub points
0%
popularity

Publisher

unverified uploader

A flutter plugin for Text to Speech. This plugin is supported on iOS, Android, Web, & macOS.

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on tts_plugin