How to play HLS video in Android?

半腔热情 提交于 2019-12-07 15:26:30

问题


Here is stream video from justin.com or twitch.tv this CHANNEL
Go to here and choose any live stream, get from them url and add html://.../hls in screen appears

Guys told me that:

Android is weird in that it will not work on Android Chrome but will work in the stock Android browser (well, previous stock Android browser now that Chrome is the stock browser). It'll also work on all Safari browsers.

I've implemented this code

 stream = (WebView) findViewById(R.id.webView1);
stream.getSettings().setJavaScriptEnabled(true);
stream.getSettings().setPluginsEnabled(true);
stream.setBackgroundColor(Color.parseColor("#000000"));
stream.getSettings().setUseWideViewPort(true);
stream.setWebViewClient(new HelloWebViewClient());
stream.getSettings().setAppCacheEnabled(true);
stream.getSettings().setDomStorageEnabled(true);
stream.getSettings().setSaveFormData(true);
stream.getSettings().setLoadWithOverviewMode(true);
stream.getSettings().setUseWideViewPort(true);
stream.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
stream.setScrollbarFadingEnabled(false);
stream.loadUrl("http://www.twitch.tv/arteezy/hls");
stream.setVisibility(View.VISIBLE);

private class HelloWebViewClient extends WebViewClient 
{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    {
        view.loadUrl(url);
        return true;
    }
}

But it opens first picture from video and stop caching... it seems like WebView doesn't cache video... What is wrong?

UPDATED

after following code stream plays only 2 sec and then stops

  stream = (WebView) findViewById(R.id.webView1);
            stream.getSettings().setJavaScriptEnabled(true);
            stream.getSettings().setJavaScriptEnabled(true);

            stream.getSettings().setPluginsEnabled(true);

            stream.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

            stream.setBackgroundColor(Color.parseColor("#000000"));
            stream.getSettings().setUseWideViewPort(true);
            // mWebView.loadUrl("data/game.swf");
            //mWebViewChat.getSettings().setBuiltInZoomControls(true);
            // mWebView.loadUrl("file:///android_asset/live.swf?channel=d2l");
            stream.setWebViewClient(new HelloWebViewClient());
             final Activity MyActivity = this;
            stream.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress)   
                {
                 MyActivity.setTitle("Loading...");
                 MyActivity.setProgress(progress * 100); 
                    if (progress*100 == 100){
                         stream.clearFormData();
                         stream.clearHistory();
                        stream.clearCache(true);
                    }

                  }
                });
            stream.getSettings().setAppCacheEnabled(true);
            stream.getSettings().setDomStorageEnabled(true);
            stream.getSettings().setSaveFormData(true);

            stream.getSettings().setLoadWithOverviewMode(true);
            stream.getSettings().setUseWideViewPort(true);

            stream.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            stream.setScrollbarFadingEnabled(false);
             stream.clearFormData();
                stream.clearHistory();
                stream.clearCache(true);

                stream.getSettings().setAppCacheEnabled(true);
                stream.getSettings().setDatabaseEnabled(true);
                stream.getSettings().setDomStorageEnabled(true);
            stream.getSettings().setPluginsEnabled(true);

            stream.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            stream.getSettings().setPluginState(PluginState.ON);
            stream.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);// no need I think

            stream.getSettings().setLoadWithOverviewMode(true);
            stream.getSettings().setUseWideViewPort(true);
            stream.getSettings().setBuiltInZoomControls(true);
            stream.setInitialScale(1);

           stream.setWebChromeClient(new WebChromeClient());
stream.getSettings().setJavaScriptEnabled(true);

    stream.getSettings().setPluginsEnabled(true);

    stream.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    stream.setBackgroundColor(Color.parseColor("#000000"));
    stream.getSettings().setUseWideViewPort(true);
    // mWebView.loadUrl("data/game.swf");
    //mWebViewChat.getSettings().setBuiltInZoomControls(true);
    // mWebView.loadUrl("file:///android_asset/live.swf?channel=d2l");
    stream.setWebViewClient(new HelloWebViewClient());
    stream.getSettings().setAppCacheEnabled(true);
    stream.getSettings().setDomStorageEnabled(true);
    stream.getSettings().setSaveFormData(true);

    stream.getSettings().setLoadWithOverviewMode(true);
    stream.getSettings().setUseWideViewPort(true);

    stream.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    stream.setScrollbarFadingEnabled(false);
     stream.clearFormData();
        stream.clearHistory();
        stream.clearCache(true);

        stream.getSettings().setAppCacheEnabled(true);
        stream.getSettings().setDatabaseEnabled(true);
        stream.getSettings().setDomStorageEnabled(true);
    stream.loadUrl("http://www.twitch.tv/arteezy/hls");

回答1:


This code works for me: (Android 5.1 Nexus 4)

public class TwitchFragment extends Fragment implements View.OnClickListener {

@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                         final Bundle savedInstanceState) {
    final View root = inflater.inflate(R.layout.fragment_twitch, null, false);

    Button buttonTwitch = (Button) root.findViewById(R.id.buttonTwitch);
    buttonTwitch.setOnClickListener(this);

    return root;
}

@Override
public void onClick(final View v) {
    FrameLayout layout = (FrameLayout) getView();
    assert layout != null;
    layout.removeView(v);

    WebView webView = new WebView(ESportLiveScoreApplication.getInstance());

    layout.addView(webView);

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setUseWideViewPort(false);
    webSettings.setDomStorageEnabled(true);
    webSettings.setSaveFormData(true);
    webSettings.setLoadWithOverviewMode(true);

    final WebChromeClient webChromeClient = new WebChromeClient();

    webView.setWebChromeClient(webChromeClient);
    webView.loadUrl("http://www.twitch.tv/h2p_gucio/hls");
}
}

I don't know why but it work only when i put WebView into FrameLayout :)



来源:https://stackoverflow.com/questions/20473757/how-to-play-hls-video-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!