嵌入Android的HTML5交互式视频

时间:2018-03-21 14:55:37

标签: android html5-video mediaelement.js

我是android的新手,我要求知道是否有任何方法可以将HTML5 interactive videosMediaelement.js结合在android studio中。我已成功将其作为Web应用程序实现,但当我将其嵌入android webview时,它并没有成功。我想在android平台上展示交互性。如果有任何帮助或示例建议,我将很高兴,谢谢

示例代码

    public class ExploreCourseActivity extends AppCompatActivity  {
        private static final String TAG = ExploreCourseActivity.class.getSimpleName();
        private boolean haveNetworkConnection() {
            boolean haveConnectedWifi = false;
            boolean haveConnectedMobile = false;

            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo[] netInfo = cm.getAllNetworkInfo();

            for (NetworkInfo ni : netInfo) {
                if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                    if (ni.isConnected())
                        haveConnectedWifi = true;
                if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                    if (ni.isConnected())
                        haveConnectedMobile = true;
            }
            return haveConnectedWifi || haveConnectedMobile;
        }


        private WebView webView;
        private String  section_id;
        String course_id, email;
        public static final String SHARED_PREF_NAME = "myloginapp";
        SharedPreferences pref;
       // @SuppressLint("NewApi")
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


            setContentView(R.layout.activity_course_view);
            Intent myIntent = getIntent();
            section_id = myIntent.getStringExtra("section_id");
            course_id = myIntent.getStringExtra("course_id");
            pref = getSharedPreferences(SHARED_PREF_NAME, MODE_PRIVATE);
            email =pref.getString(EMAIL_SHARED_PREF,null);

            //Get webview
            webView = (WebView) findViewById(R.id.webView1);
            if(haveNetworkConnection()){
                startWebView( "http://10.0.2.2/ymvssas/mobi/media.php?sid="+section_id+"&cid="+course_id+"&uid="+email);
            } else {
                webView.loadUrl("file:///android_assets/error.html");
            }
        }

       // @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        private void startWebView(String url) {

            //Create new webview Client to show progress dialog
            //When opening a url or click on link

            webView.setWebViewClient(new WebViewClient() {
                ProgressDialog progressDialog;


                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }


                //Show loader on url load
                public void onLoadResource (WebView view, String url) {
                    if (progressDialog == null) {
                        // in standard case YourActivity.this
                        progressDialog = new ProgressDialog(ExploreCourseActivity.this);
                        progressDialog.setMessage("Loading...");
                        progressDialog.show();
                    }
                }
                public void onPageFinished(WebView view, String url) {
                    try{
                        if (progressDialog.isShowing()) {
                            progressDialog.dismiss();
                            // progressDialog = null;
                        }
                    }catch(Exception exception){
                        exception.printStackTrace();
                    }
                }

            });

            // Javascript inabled on webview
            WebSettings ws = webView.getSettings();
            webView.getSettings().setDomStorageEnabled(true);

            webView.getSettings().setAppCacheMaxSize(1024*1024*8);

            webView.getSettings().setAllowFileAccess(true);
            webView.getSettings().setAppCacheEnabled(true);

            webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
            webView.setWebChromeClient(new WebChromeClientCustomPoster());
            webView.getSettings().setPluginState(WebSettings.PluginState.ON);
           webView.getSettings().setJavaScriptEnabled(true);
                        webView.getSettings().setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webView.getSettings().setDatabaseEnabled(true);
            webView.getSettings().setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");



            //Load url in webview
            webView.loadUrl(url);


        }



        // Open previous opened link from history on webview when back button pressed

        @Override
        // Detect when the back button is pressed
        public void onBackPressed() {
            if(webView.canGoBack()) {
                webView.goBack();
            } else {
                // Let the system handle the back button
                super.onBackPressed();



            }
        }

        private class WebChromeClientCustomPoster extends WebChromeClient {
            @Override
            public Bitmap getDefaultVideoPoster() {
                return Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
            }
        }


    }

1 个答案:

答案 0 :(得分:0)

使用WebChromeClient并在清单中设置android:hardwareAccelerated="true"

mWebView.setWebChromeClient(new WebChromeClient());

同样如下:

mWebView.getSettings().setJavaScriptEnabled(true);