webview中的视频无法播放

时间:2012-12-24 11:50:20

标签: android-webview

嘿伙计们我是Android新手。我在webview中的视频无法在移动设备上播放。但它确实在平板电脑上播放。 会有什么问题?以下是我的代码。请帮帮我。

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.PluginState;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class WebViewActivity extends Activity {

    WebView web;
    ImageView img,bckimg,share_btn;
    String content,shareUrl;
    String movYear, movName, movImage, movDirector, movAwards; 
    String movProducer, movActedAs, movAnecdotes, movSynopsis; 
    boolean thisUrl = false;
    ImageView showProgress;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_web_view);


        Bundle bundle=getIntent().getExtras();
        showProgress=(ImageView)findViewById(R.id.showProgress);

        img = (ImageView)findViewById(R.id.header);
        bckimg = (ImageView) findViewById(R.id.wbview_bckbtn);
        share_btn = (ImageView) findViewById(R.id.share_btn);

        web=(WebView)findViewById(R.id.webview);
        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setPluginsEnabled(true);
        web.getSettings().setPluginState(PluginState.ON);
        web.getSettings().setAllowFileAccess(true);
        web.getSettings().setBuiltInZoomControls(true);
        web.getSettings().setBlockNetworkLoads(false);
        web.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");

        web.setWebViewClient(new MyWebViewClient());

        if(bundle!=null)
        {
            int tab_value = bundle.getInt("tab_value");

            if(tab_value==1){
                img.setImageResource(R.drawable.bar_latest);

                thisUrl = false;
            }else if(tab_value==2){
                img.setImageResource(R.drawable.bar_dance);
                thisUrl = false;
            }else if(tab_value==3){
                img.setImageResource(R.drawable.bar_movies);
                thisUrl = true;
            }else if(tab_value==4){
                img.setImageResource(R.drawable.bar_fashion);
                thisUrl = false;
            }else if(tab_value==5){
                img.setImageResource(R.drawable.bar_health);
                thisUrl = false;

            }else if(tab_value==6){
                img.setImageResource(R.drawable.bar_art);
                thisUrl = false;
            }


            if(!thisUrl){
                share_btn.setVisibility(View.VISIBLE);
                content =   bundle.getString("content");
                web.loadData("<!DOCTYPE html><head><meta charset=\"UTF-8\"/><meta name=\"viewport\" content=\"width=device-width\" /><title>Madhuri Dixit-Nene</title><link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"http://madhuridixit-nene.com/wp/wp-content/themes/twentyeleven/style.css\" /><style type=\"text/css\">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style><style type=\"text/css\">p {padding:5px !important;margin:5px !important;}</style></head><body class=\"single single-post postid-1606 single-format-standard single-author singular two-column right-sidebar\"><div class=\"entry-content\">"+content+"</div></body></html>","text/html; charset=UTF-8", null);
                shareUrl=bundle.getString("shareUrl");
            }else{
                share_btn.setVisibility(View.GONE);
                movYear     =   bundle.getString("movieYear");
                movName     =   bundle.getString("movieName");
                movImage        =   bundle.getString("movieImage");
                movAwards       =   bundle.getString("movieAwards");
                movDirector =   bundle.getString("movieDirector");
                movProducer =   bundle.getString("movieProducer");
                movActedAs  =   bundle.getString("movieActedAs");
                movAnecdotes    =   bundle.getString("movieAnecdotes");
                movSynopsis =   bundle.getString("movieSynopsis");

                web.loadData("<p style='text-align:center'><img class='aligncenter size-full wp-image-1607' title='' src="+movImage+" alt='' width='240px' height='180px' /></p><p><center><U><H2>"+movName+"("+movYear+")</H2></U></center></p><p><strong>Director : </strong>"+movDirector+"</p><p><strong>Producer : </strong>"+movProducer+"</p><p><strong>Character : </strong>"+movActedAs+"</p><p><strong>Summary : </strong>"+movAnecdotes+"</p><p><strong>Synopsis : </strong>"+movSynopsis+"</p>\n","text/html; charset=UTF-8", null);
                shareUrl=bundle.getString("shareUrl");
            }
        }

        bckimg.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
                overridePendingTransition(R.anim.slide_out_left,R.anim.slide_out_right);
            }
        });

        share_btn.setOnClickListener(new OnClickListener() {

            @Override


            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Madhuri");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareUrl);
                startActivity(Intent.createChooser(sharingIntent, "Share via"));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_web_view, menu);
        return true;
    }

    class MyWebViewClient extends WebViewClient {
        @Override
        // show the web page in webview but not in web browser
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.getSettings().setJavaScriptEnabled(true);
            view.getSettings().setPluginsEnabled(true);
            view.getSettings().setPluginState(PluginState.ON);
            view.getSettings().setAllowFileAccess(true);
            view.getSettings().setBuiltInZoomControls(true);
            view.getSettings().setBlockNetworkLoads(false);
            view.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
            view.loadUrl(url);

            return true;
        }



        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            showProgress.clearAnimation();
            showProgress.setVisibility(View.GONE);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
            showProgress.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate));

            showProgress.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        finish();
        overridePendingTransition(R.anim.slide_out_left,R.anim.slide_out_right);
        /*  super.onBackPressed();*/

    }
}

并且还向我解释了WebViewClient和WebViewChromeClient之间的区别是什么?

1 个答案:

答案 0 :(得分:0)

请在您的android清单文件中添加hardwareaccelerated设置为true。 android 3.0+我们必须启用此功能,然后才支持视频。你当然可以使用它们,它们只是具有不同的功能。设置您自己的自定义WebViewClient可以处理onPageFinished,shouldOverrideUrlLoading等,WebChromeClient允许您处理Javascript的alert()和其他功能。