在Android Webview上显示视频。视频在播放时变黑

时间:2014-11-28 11:36:53

标签: java android webview youtube html5-video

在Android Webview上显示视频。播放和声音时视频变黑,但视频屏幕变黑。我正在研究android 15+版本。发挥这个需要什么?

Fragment_video.xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="children.apps.Fragment.FragmentVideo">


    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webVideoView"
        />

</FrameLayout>

Java Class

public class FragmentVideo extends Fragment {


    public FragmentVideo() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        WebView webView;
        View view = inflater.inflate(R.layout.fragment_video,container,false);

        //ArrayList<String> result = new ArrayList<String>();

        webView = (WebView) view.findViewById(R.id.webVideoView);
        webView.getSettings().setJavaScriptEnabled(true);
        WebSettings ws = webView.getSettings();
        ws.setPluginState(WebSettings.PluginState.ON);
        ws.setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient()
          {
         @Override
            public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
            {
                    // what to write
            }
            @Override
            public void onHideCustomView(){

        }
            @Override
            public View getVideoLoadingProgressView()
            {

                return null;
            }

        });

        String htmlhead = "<html><head></head><body>";
        String htmlbody = "";

        htmlbody += "<div class=\"video-shortcode\"><iframe title=\"YouTube video player\" width=\"300\"" +
                "height=\"200\" src=\"https://www.youtube.com/embed/HXrcUyCVA6c\"" +
                "frameborder=\"0\" allowfullscreen></iframe></div>";

        String htmlfoot = "</body></html>";
        String html = htmlhead+htmlbody+htmlfoot;
        webView.loadData(html, "text/html","utf-8");
        return view;
    }


}

0 个答案:

没有答案