How to fix it: App allows you to play background YouTube videos

I made a phonegap app that plays YouTube videos. Google pulled it out of the play store because "the app lets you play YouTube videos in the background."

I have no idea what this means.

Can someone help me fix this so that the videos don't play in the background?

Thank.

+3


source to share


4 answers


I think google means you need to pause youtube video when the app is in the background (like touching the home button of the device)

how i solve it, register a "pause" event (called when the app goes to the background)

document.addEventListener("pause", pause, false);
function pause (argument) {
  if (typeof document.app.player != "undefined") {
    document.app.player.pauseVideo();
  }

}

      



When playing youtube, I keep the link to the player:

        patt=/\/www\.youtube\.com\/watch\?v=(.*)/;
        m = patt.exec(url);
        if (m.length == 2) {
            url = "http://www.youtube.com/embed/"+ m[1] //+ "?autoplay=1"
            console.log("url = "+url)

        }

        YTid = 'yt_'+m[1];

        $("#MediaPPSVideo").html(
            "<div id='"+YTid+"' width=\"100%\"></div>"
        ).after(function() {
            document.app.player = new YT.Player(YTid, {
              height: $(window).height()/2,
              width: $(window).width()*0.95,
              videoId: m[1],
              events: {
                'onReady': onPlayerReady,
              }
            });
        })

      

this one is ugly but working

+2


source


add this to config.xml file, Works for me Cordova 6.4.0.

<preference name="KeepRunning" value="false" />

      



make changes to the below activity public class MainActivity extends CordovaActivity

@Override
protected CordovaWebViewEngine makeWebViewEngine() {
    return new WebViewEngine(this, preferences);
}


static class WebViewEngine extends SystemWebViewEngine {

    public WebViewEngine(Context context, CordovaPreferences preferences) {
        super(context, preferences);
    }


    @Override
    public void setPaused(boolean value) {
        super.setPaused(value);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return;
        } else if (value) {
            webView.onPause();
        } else {
            webView.onResume();
        }
    }
}

      

+1


source


There could be a couple of things here:

  • I wonder if Google really means "background play" ... or if they really mean just "play" all together. They may require users to be redirected to YouTube in order to play them. Perhaps Google wants YouTube to be the only YouTube video player.

  • Hopefully, you most likely need to make sure that when your application exits / terminates / pauses, you also force close / stop the player in your application. If you are using a webView, it can keep playing videos even after your application is not visible.

0


source


In order for HTML video to stop playing when the application is in the background, you need to pause the WebView timers using KeepRunning

in the config.xml file:

<preference name="KeepRunning" value="false" />

      

and also call WebView's onPause method when your activity is paused. For this with Cordova Android 4.0.0, you can use your own CordovaWebViewEngine implementation:

public class MainActivity extends CordovaActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();

        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
    }

    @Override
    protected CordovaWebViewEngine makeWebViewEngine() {
        return new WebViewEngine(this, preferences);
    }

    /**
     * Custom Engine implementation so it can pass resume and pause events to WebView.
     * This is necessary to stop HTML5 video when app is put to background.
     */
    static class WebViewEngine extends SystemWebViewEngine {

        public WebViewEngine(Context context, CordovaPreferences preferences) {
            super(context, preferences);
        }

        public WebViewEngine(SystemWebView webView) {
            super(webView);
        }

        @Override
        public void setPaused(boolean value) {
            super.setPaused(value);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                return;
            } else if (value) {
                webView.onPause();
            } else {
                webView.onResume();
            }
        }
    }

}

      

0


source







All Articles