Displaying video on Android Webview. Video turns black while playing

Displaying video on Android Webview. The video turns black during playback and audio, but the video screen turns black. I am working on android 15+ versions. what is needed for this?

Fragment_video.xml file

<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;
    }


}

      

+3


source to share





All Articles