Android webview直播视频几分钟后停止播放

时间:2017-03-25 16:48:31

标签: javascript android webview live

我制作了一个关于直播电视节目的应用。它运行良好,但几分钟后它会自动停止。我点击屏幕但它不起作用。我已经展示了我的代码。如果有任何要添加或删除的内容请告诉我。我已经尝试但失败了。

package ******************************.thelivetv;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class MainActivity extends Activity {
    InterstitialAd mInterstitialAd;

    private InterstitialAd interstitial;

    private WebView webView;


    String url2 = "file:///android_asset/error.html";
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        webView = new WebView(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(webView);



        final WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setPluginState(WebSettings.PluginState.ON);

        if (isOnline(MainActivity.this)) {
            webView.setWebViewClient(new WebViewClient() {
                // autoplay when finished loading via javascript injection
                public void onPageFinished(WebView view, String url) {
                    webView.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()");
                }
            });
            webView.setWebChromeClient(new WebChromeClient());
            web.setWebViewClient(new WebViewClient() {

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });
            webView.loadUrl("http://shafatillullah.com/blackboy/channel9/");
        } else {
            String message = "Internet connection not available. Try later";
            new AlertDialog.Builder(MainActivity.this)
                    .setMessage(message)
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    })
                    .show();


        }



        //full screen ad

        final AdRequest adRequest = new AdRequest.Builder().build();


        // Prepare the Interstitial Ad
        interstitial = new InterstitialAd(MainActivity.this);
        // Insert the Ad Unit ID
        interstitial.setAdUnitId(getString(R.string.bigscreen));

        interstitial.loadAd(adRequest);
        // Prepare an Interstitial Ad Listener
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                // Call displayInterstitial() function
                displayInterstitial();

            }
        });
        //end ad view



    }

    //Admob ad load
    public void displayInterstitial() {
        // If Ads are loaded, show Interstitial else show nothing.
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }




    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
            super.onReceivedError(view, errorCode, description, failingUrl);

            view.loadUrl(url2);
        }
    }


    // For full screen View
    class FullScreenActivity {
        public void invoke() {
            // remove title
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onPause() {
        super.onPause();
        webView.onPause();
    }



    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onResume() {
        super.onResume();
        webView.onResume();
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
    public static boolean isOnline(Context c) {
        boolean result = false;

        ConnectivityManager cm = (ConnectivityManager) c.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ninfo = cm.getActiveNetworkInfo();
        if (ninfo != null) {
            result = ninfo.isConnected();
        }

        return result;
    }



}

1 个答案:

答案 0 :(得分:-1)

我了解这个问题不是按照社区准则,而是请帮助其他程序员。

https://bugs.chromium.org/p/chromium/issues/detail?id=415344中讨论了WebView中的流传输问题,因此我认为使用Android的Media Player https://github.com/assen-totin/android-icecast-player/blob/master/src/com/voody/icecast/player/StationListenService.java

会更好。

荣誉!