4

I made a phonegap app that plays YouTube videos. Google has pulled it from the play store because the "app enables background playing of YouTube videos."

I have no idea what that means.

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

Thanks.

4 Answers4

2

I think that Google means that u have to pause the youtube video when the app is in background mode (e.g. touching the device home button)

the way I solve it so register the 'pause' event (called when the app goes to background)

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

}

when a youtube is played I keep the reference of 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 ugly but working

Elia Weiss
  • 8,324
  • 13
  • 70
  • 110
1

add this in config.xml file, Worked for me Cordova 6.4.0.

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

make the changes in 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();
        }
    }
}
0

A couple things might be here:

  1. I wonder if Google really means "background playing" ... or do they really mean simply "playing" all together. They might require that you redirect users to YouTube in order to play them. It's possible that Google wants YouTube to be the only YouTube video player app.

  2. Hopefully more likely is --- you need to ensure that when your application exits/ends/pauses that you also forcibly close/stop the player in your app. If you're using a webView, it might keep playing the video even once your app is not visible.

Steve Kennedy
  • 5,312
  • 3
  • 26
  • 41
0

For HTML video to stop playing when the app is in the background you need to pause WebView timers using KeepRunning preference in config.xml:

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

and also call WebView's onPause method when your Activity is paused. To do this using Cordova Android 4.0.0 you can use your custom 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();
            }
        }
    }

}
krebernisak
  • 950
  • 7
  • 16
  • This changes in the java file gives all sort of errors when building the app. Do we need to include a special import? – carol1287 Jun 17 '15 at 08:55
  • Nothing special, just make sure you are running Cordova Android 4.0.0+ and add all required imports: `org.apache.cordova.engine.SystemWebView`, `org.apache.cordova.engine.SystemWebViewEngine`, `org.apache.cordova.CordovaWebViewEngine`, etc. – krebernisak Jun 17 '15 at 09:57