Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When app's AppLifecycleState is inactive I want to stop both speech_to_text and flutter_tts #521

Open
pandabossli opened this issue Aug 14, 2024 · 0 comments

Comments

@pandabossli
Copy link

💬 Questions and Help

Provide question related to this flutter plugin.
`import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_tts/flutter_tts.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;
import 'package:speech_to_text/speech_to_text.dart';
import '../events/app_state_event.dart';
import '../events/init_event.dart';
import '../events/speech_to_text_event.dart';
import '../utils/my_speech_to_text.dart';

class TestPage extends StatefulWidget {
const TestPage({Key? key}) : super(key: key);

@OverRide
_TestPageState createState() => _TestPageState();
}

class _TestPageState extends State {
late FlutterTts flutterTts;
late stt.SpeechToText speech;
String message = "";
@OverRide
void initState() {
InitEvent.instance().on().listen((event) async {
if (event.status == AppLifecycleState.inactive) {
// deactivate();
setState(() {
message = "inactive";
});
await flutterTts.pause();
await speech.stop();
} else if (event.status == AppLifecycleState.resumed) {
// initState();
setState(() {
message = "resumed";
});
addListen();
}
});

initText2Speech();
initSpeech2Text();

super.initState();

}

Future initText2Speech() async {
flutterTts = FlutterTts();
if (Platform.isIOS) {
await flutterTts.setSharedInstance(true);
await flutterTts.setIosAudioCategory(
IosTextToSpeechAudioCategory.playAndRecord,
[
IosTextToSpeechAudioCategoryOptions.defaultToSpeaker,
],
);
}
await flutterTts.awaitSpeakCompletion(true);

flutterTts.setCompletionHandler(() {
  //文字转语音完毕

  addListen();
});

}

Future initSpeech2Text() async {
await MySpeechToText.init();
speech = MySpeechToText.speech!;

InitEvent.instance().on<SpeechToTextEvent>().listen(
  (event) {
    // 声音转文字状态
    print(event.status);
    if (event.status == 'done') {
      flutterTts.speak(message);
    }
  },
);
addListen();

}

addListen() async {
await speech.listen(
listenFor: Duration(seconds: 60),
pauseFor: Duration(seconds: 3),
listenMode: ListenMode.dictation,
onResult: (result) {
setState(() {
message = result.recognizedWords;
});
},
onDevice: false,
partialResults: true,
);
}

@OverRide
Widget build(BuildContext context) {
return Container(
width: double.infinity,
height: double.infinity,
alignment: Alignment.center,
child: Text(
'$message',
style: TextStyle(
fontSize: 28.sp,
color: Colors.white,
),
),
);
}
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant