I am trying to keep the WebView media content from stopping when user exits or when screen is locked.
I follow this solutions here but it still stops the media from playing.
WebView media content stops when the screen is locked ( activity's onStop is called)
How can I make WebView keep a video or audio playing in the background?
the main bits of my code is something like this:
public class WebApp extends AppCompatActivity {
WebView webView;
WebSettings webSettings;
String ua = "Mozilla/5.0 (Linux; Android 10; Google Pixel 4 Build/QD1A.190821.014.C2; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.108 Mobile Safari/537.36";
public static String url = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Objects.requireNonNull(getSupportActionBar()).hide();
getWindow().setStatusBarColor(getColor(R.color.black));
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_nitter);
webView = findViewById(R.id.webview);
webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyChrome());
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(url);
}
public class MyWebView extends WebView {
public MyWebView(Context context) {
super(context);
}
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
if (visibility != View.GONE) super.onWindowVisibilityChanged(visibility);
}
}
@Override
public void onBackPressed(){
if(webView.canGoBack())
{
webView.goBack();
}
else
{
webView.clearHistory();
super.onBackPressed();
}
}
would anyone be able to tell me what am I doing wrong even though I think I followed the solution correctly? Thank You.