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 videos playing on background when device is inactive. Which cause playstore to reject app #372

Closed
PaulVinSoft249 opened this issue Jun 2, 2020 · 11 comments

Comments

@PaulVinSoft249
Copy link

PaulVinSoft249 commented Jun 2, 2020

Environment

Flutter version: 1.17.0
Plugin version: flutter_inappwebview 3.3.0
Android version: 9.0
iOS version:---
Xcode version: ---
Device information: techno

Description

Expected behavior:

Current behavior:

Steps to reproduce

  1. This
  2. Than that
  3. Then

Images

Stacktrace/Logcat

@pichillilorenzo
Copy link
Owner

For Android, you can use InAppWebViewController.android.pause and InAppWebViewController.android.resume to pause and resume WebView.
You should implement WidgetsBindingObserver in your Widget and check AppLifecycleState state through didChangeAppLifecycleState() method.
If you need to pause/resume also JavaScript execution you can use InAppWebViewController.pauseTimers/InAppWebViewController.resumeTimers.

Check the API docs if you need more detail.

Howevert, here is an example with a YouTube video:

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  InAppWebViewController webView;

  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print('state = $state');
    if (webView != null) {
      if (state == AppLifecycleState.paused) {
        webView.pauseTimers();
        if (Platform.isAndroid) {
          webView.android.pause();
        }
      } else {
        webView.resumeTimers();
        if (Platform.isAndroid) {
          webView.android.resume();
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('InAppWebView Example'),
        ),
        body: Container(
            child: Column(children: <Widget>[
          Expanded(
              child: InAppWebView(
            initialUrl: "https://www.youtube.com/watch?v=NfNdXgJZfFo",
            initialHeaders: {},
            initialOptions: InAppWebViewGroupOptions(
              crossPlatform: InAppWebViewOptions(
                debuggingEnabled: true,
              ),
            ),
            onWebViewCreated: (InAppWebViewController controller) {
              webView = controller;
            },
            onLoadStart: (InAppWebViewController controller, String url) {},
            onLoadStop: (InAppWebViewController controller, String url) {},
          ))
        ])),
      ),
    );
  }
}

@PaulVinSoft249
Copy link
Author

inAppWebview flickering anyway to go around that?

@pichillilorenzo
Copy link
Owner

What do you mean? Can you make an example/screen record?

@PaulVinSoft249
Copy link
Author

I mean it flickers when keyboard show ups any way to go around that

@pichillilorenzo
Copy link
Owner

Unfortunately no. This is related to how Flutter embeds Android native views inside its widget tree.

I know that somebody is working on another way to embed Android native views.
I hope it will be better than what we have right now!

@PaulVinSoft249
Copy link
Author

Ok awesome thank very helpfull

This was referenced Jul 6, 2020
@xcc3641
Copy link

xcc3641 commented Oct 13, 2020

@thang510023 have u ever fixed bug when playing mp3 ?

@thegrxp
Copy link

thegrxp commented Dec 11, 2020

This solution is working on Android but still have the issue on iOS

@thegrxp
Copy link

thegrxp commented Dec 11, 2020

I got it working for both android and iOS by having webView.pauseTimers(); and webView.resumeTimers(); only set for Android. The Youtube video stops when you open a new app or when you are on the launcher, but continues if you are sliding between opened apps until you select one (same behavior as Youtube).

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  InAppWebViewController webView;

  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print('state = $state');
    if (webView != null) {
      if (state == AppLifecycleState.paused) { 
        if (Platform.isAndroid) {
          webView.pauseTimers(); // Pause timers only for android
          webView.android.pause();
        }
      } else {
        if (Platform.isAndroid) {
          webView.resumeTimers(); // Resume timers only for android
          webView.android.resume();
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('InAppWebView Example'),
        ),
        body: Container(
            child: Column(children: <Widget>[
          Expanded(
              child: InAppWebView(
            initialUrl: "https://www.youtube.com/watch?v=NfNdXgJZfFo",
            initialHeaders: {},
            initialOptions: InAppWebViewGroupOptions(
              crossPlatform: InAppWebViewOptions(
                debuggingEnabled: true,
              ),
            ),
            onWebViewCreated: (InAppWebViewController controller) {
              webView = controller;
            },
            onLoadStart: (InAppWebViewController controller, String url) {},
            onLoadStop: (InAppWebViewController controller, String url) {},
          ))
        ])),
      ),
    );
  }
}

@UjjwaRaijada
Copy link

Unfortunately no. This is related to how Flutter embeds Android native views inside its widget tree.

I know that somebody is working on another way to embed Android native views.
I hope it will be better than what we have right now!

I must say, you guys have done an amazing job.

Copy link

github-actions bot commented Oct 7, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants