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

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

Open
tuyennvt opened this issue Jan 9, 2024 · 13 comments
Open

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

tuyennvt opened this issue Jan 9, 2024 · 13 comments

Comments

@tuyennvt
Copy link

tuyennvt commented Jan 9, 2024

I'm facing the same problem #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
Copy link
Author

tuyennvt commented Jan 9, 2024

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
Copy link
Contributor

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
Copy link
Author

tuyennvt commented Jan 9, 2024

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
Copy link
Contributor

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
Copy link
Author

tuyennvt commented Jan 9, 2024

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
Copy link

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
Copy link
Contributor

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

@wamynobe
Copy link
Contributor

@xweng1016 Have you found any workaround?

@kuberanb
Copy link

kuberanb commented Feb 6, 2024

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
Copy link
Contributor

wamynobe commented Feb 6, 2024

@kuberanb Can you describe more detail about your case?

@kuberanb
Copy link

kuberanb commented Feb 7, 2024

@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
Copy link
Contributor

wamynobe commented Feb 7, 2024

@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
Copy link
Contributor

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

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

No branches or pull requests

5 participants