0

I have started building my own app to connect to my home media server with jellyfin.

The basic goal of this app is to allow the automation of enabling disabling VPNs and handling the IP's to the server. I got most of the things to work very well, even though it's my first android app. But if I turn the screen of my device off, or start another application on top the audio/video stops playing. Is there any simple way to implement this? Maybe if it is "easy" something like in Chrome itself would also be very nice: Popup on Chrome

As I read through the questions already asked about this topic i think i anyhow need to run the app as service, but have failed to get it to work.

webview audio play on background

MainActivity:

package com.example.jellyfin_beta;
 
import [needed stuff]
public class MainActivity extends AppCompatActivity {
 
        // private static final String HOME_WIFI_SSID = ""; 
    private static final String HOME_WIFI_IP_PREFIX = "192.168.1."; 
    private static final String SERVER_IP_HOME_WIFI = "http://192.168.1.111:8096/"; 
    private static final String SERVER_IP_VPN = "http://:8096/"; 
 
    private WebView webView;
    private View decorView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.webview);
        webView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = webView.getSettings();
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        checkNetworkAndLoadURL();
 
        decorView = getWindow().getDecorView();
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                if (visibility == 0)
                    decorView.setSystemUiVisibility(hideSystemBars());
            }
        });
    }
        @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
        @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            decorView.setSystemUiVisibility(hideSystemBars());
        }
    }
    private int hideSystemBars() {
        return View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    }
    private class MywebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }
    }
 
    @Override
    public void onBackPressed() {
        if (webView.isFocused() && webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
 
    @Override
    protected void onDestroy() {
               super.onDestroy();
        Intent intent = new Intent("com.tailscale.ipn.DISCONNECT_VPN");
        intent.setClassName("com.tailscale.ipn", "com.tailscale.ipn.IPNReceiver");
        sendBroadcast(intent);
    }
    private void checkNetworkAndLoadURL() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected()) {
            if (isConnectedToLocalNetwork()) {
                webView.loadUrl(SERVER_IP_HOME_WIFI);
            } else {
                Intent intent = new Intent("com.tailscale.ipn.CONNECT_VPN");
                intent.setClassName("com.tailscale.ipn", "com.tailscale.ipn.IPNReceiver");
                sendBroadcast(intent);
                webView.loadUrl(SERVER_IP_VPN);
            }
        } else {
            Toast.makeText(this, "No network connection", Toast.LENGTH_SHORT).show();
        }
    }
    private boolean isConnectedToLocalNetwork() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    if (!address.isLoopbackAddress() && address.getHostAddress().startsWith(HOME_WIFI_IP_PREFIX)) {
                        return true;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return false;
    }
}

how can i make webview keep a video or audio playing in the background This seems the most promising out of all threads to this topic, but i couldn't get this to work.

Thank you for any help in advance. And I please don't explain too complicated, it's my first time with android apps. :)

crazywolf
  • 21
  • 3

0 Answers0