csdcorp / speech_to_text

A Flutter plugin that exposes device specific text to speech recognition capability.
BSD 3-Clause "New" or "Revised" License
389 stars 238 forks source link

Youtube Player paused when calling speechToText.listen() #472

Open tuyennvt opened 10 months ago

tuyennvt commented 10 months ago

I'm facing the same problem https://github.com/csdcorp/speech_to_text/issues/374. I have created a demo repo. In this repo I tried to do what you suggested in issue 374. You can see the print() in the build() function. I didn't notice any further rebuilds after I pressed the Speech To Text Button. But the Youtube player is pause until the listening process ends. I have try on Android device. You can pull my repo and give it a try (https://github.com/tuyennvt/demo_speech_to_text). Thank you!

tuyennvt commented 10 months ago
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:speech_to_text/speech_to_text.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo Speech To Text',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Demo Speech To Text'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: const Padding(
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            _YoutubePlayerWidget(),
            SizedBox(height: 16),
            _SpeechToTextWidget(),
          ],
        ),
      ),
    );
  }
}

class _YoutubePlayerWidget extends StatelessWidget {
  const _YoutubePlayerWidget();

  @override
  Widget build(BuildContext context) {
    print('_YoutubePlayerWidget build');
    return YoutubePlayer(
      controller: YoutubePlayerController(
        initialVideoId: 'iLnmTe5Q2Qw',
        flags: const YoutubePlayerFlags(
          autoPlay: true,
        ),
      ),
      showVideoProgressIndicator: true,
    );
  }
}

class _SpeechToTextWidget extends StatefulWidget {
  const _SpeechToTextWidget();

  @override
  State<_SpeechToTextWidget> createState() => _SpeechToTextWidgetState();
}

class _SpeechToTextWidgetState extends State<_SpeechToTextWidget> {
  SpeechToText speechToText = SpeechToText();
  bool availableSpeech = false;

  @override
  void initState() {
    super.initState();
    _initSpeechToText();
  }

  Future<void> _initSpeechToText() async {
    final permissionStatus = await Permission.speech.request();
    if (!permissionStatus.isGranted) {
      return;
    }
    availableSpeech = await speechToText.initialize();
    return;
  }

  Future<void> _listenSpeechToText() async {
    if (!availableSpeech) {
      return;
    }
    speechToText.listen(
      onResult: (value) {},
      onSoundLevelChange: (value) {},
      listenMode: ListenMode.search,
    );
  }

  @override
  Widget build(BuildContext context) {
    print('_SpeechToTextWidget build');
    return FilledButton(
      onPressed: () async {
        _listenSpeechToText();
      },
      child: const Text('Speech To Text'),
    );
  }
}
sowens-csd commented 10 months ago

YouTube player sounds like a different issue than 374. Because SpeechToText takes over the audio channel while it is listening I can see why YouTube might pause. Do you have start/stop sounds configured in your app?

tuyennvt commented 10 months ago

YouTube player sounds like a different issue than 374. Because SpeechToText takes over the audio channel while it is listening I can see why YouTube might pause. Do you have start/stop sounds configured in your app?

I played the video on mute, but the problem is still not resolved. Is this the default behavior when I combine them together?

sowens-csd commented 10 months ago

I don't know, I've never tried it. Do you have the start/stop sounds configured? If you do I'd suggest trying it without them. If it still pauses that would tell us something.

tuyennvt commented 10 months ago

I don't know, I've never tried it. Do you have the start/stop sounds configured? If you do I'd suggest trying it without them. If it still pauses that would tell us something.

I don't understand. What do you want me to start/stop sound of?

xweng1016 commented 10 months ago

Probably same issue here. I'm trying to make a talking app with speech_to_text and then play the response audio with audio player or something, but once I trigger speech_to_text no other sound can be played until I reopen a new simulator, seems like this audio channel issue but I tried all the possible stop and start methods I can and no luck.

YouTube player sounds like a different issue than 374. Because SpeechToText takes over the audio channel while it is listening I can see why YouTube might pause. Do you have start/stop sounds configured in your app?

wamynobe commented 10 months ago

@sowens-csd When I call speechToText.listen(), It blocks the current playing video in video_player package also.

wamynobe commented 10 months ago

@xweng1016 Have you found any workaround?

kuberanb commented 9 months ago

have you got any solution for this , i also want to use speech to text and record the same audio and play in the app when needed

wamynobe commented 9 months ago

@kuberanb Can you describe more detail about your case?

kuberanb commented 9 months ago

@wamynobe I want the audio recording and speech_to_text to work simulataneously that is when i click the button the audio should record and store it in storage and get the text from the audio and show it in the screen . When i try to enable both at same time the speech_to_text stops listening so how to fix this , i have also mentioned you in my code

wamynobe commented 9 months ago

@kuberanb because speech_to_text is running on main thread and so do the audio media. my suggestion is that you should try another way to use speech to text. such as build your own speech to text package. I am working on a new speech to text that is spawned by another thread. You can try this approach.

wamynobe commented 8 months ago

@kuberanb to reconfirm that the audio resource using by webview or some packages like flutter_video ... conflicted with speech to text audio resource so it will pause audio or even the video.