androidwebviewandroid-webviewtwitchvideo-embedding

Playing Twitch.Tv Video within Android App? (Android Studio development)


Is there any way to natively play a Twitch stream-video in my Android app? I couldn't find any information about this on the internet.

This is how I open up a twitch video at the moment in WebView (twitch.html):

<a href="https://twitch.tv/'+element.channel.name+'/embed"> ...

But this leads to some errors when I change my device from vertical to horizontal view. Then the video sound will be played without showing the video anymore, etc.

I was thinking of a workaround-fix, if you can call that and just seperately open up the twitch-channel in the app browser by the Android user, but how can I add this exception in my code? This is in my MainActivity.java:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

It's from the official Android developer page and prevents that links will be opened up in another browser app. How could I add a exception just for "twitch.tv"?


Solution

  • The way I display the video is by using this:

        String url = "http://twitch.tv/PUT CHANNEL HERE/embed";
        WebView mWebView;
        mWebView = (WebView) findViewById(R.id.webview1);
        mWebView.setWebChromeClient(new WebChromeClient());
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        mWebView.loadUrl(url);
    

    and my layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:weightSum="1">
            <WebView
                android:id="@+id/webview1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    </LinearLayout>