3

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"?

1 Answers1

0

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>
linuxgnuru
  • 142
  • 5
  • I have tried this with a Nexus 7 (marshmellow) and a samsung galaxy S6 (lolipop) and both work fine for me however I have heard that sometimes people get the flash version (https://discuss.dev.twitch.tv/t/embed-player-in-webview-android/2763) what /embed does is "This will embed both HTML5 and Flash, as appropriate" acording to (https://discuss.dev.twitch.tv/t/html5-embedded-player/150/17) – linuxgnuru Jan 16 '16 at 10:23
  • finaly i found solution. webView.loadUrl("http://www.twitch.tv/" + channel + "/popout"); works perfectly for all versions – Android Android Jan 18 '16 at 07:21