Run youtube videos in a web view

Asked

Viewed 141 times

0

Youtube videos do not play in A web view on Android 4.0, but in 6.0 videos run normally. I’ve done a lot of research on this topic, but I haven’t found any practical answers to my very low knowledge of Android development. If anyone is able to help me how to play Youtube videos in a web view, please be very clear. to follow my code:

    pagina.loadUrl("https://www.google.com/");
    pagina.getSettings().setJavaScriptEnabled(true);
    pagina.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);


    pagina.setWebViewClient(new WebViewClient(){

            public  void onPageStarted(WebView view, String url, Bitmap favicon)    

            {
                super.onPageStarted(view, url, favicon); 
                Barra.setText(url);
                pb.setVisibility(View.VISIBLE);}

            @Override
            public void onPageFinished(WebView view, String url)
            {
                pb.setVisibility(View.INVISIBLE);}});
    pagina.setDownloadListener(new DownloadListener(){
            @SuppressLint("InlinedApi")public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
            {
                DownloadManager.Request request =new DownloadManager.Request(Uri.parse(url));                
                request.allowScanningByMediaScanner();final String filename =URLUtil.guessFileName(url, contentDisposition, mimetype);          
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);               
                request.setDestinationInExternalPublicDir("/Download", filename);DownloadManager dm =(DownloadManager) getSystemService(DOWNLOAD_SERVICE);          
                dm.enqueue(request);Intent intent =new Intent(Intent.ACTION_OPEN_DOCUMENT);             
                intent.addCategory(Intent.CATEGORY_OPENABLE);    
                intent.setType("*/*");
                Toast.makeText(getApplicationContext(), "Download iniciado",
                               Toast.LENGTH_LONG).show();}});

    proximo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if (pagina.canGoForward())
                {
                    pagina.goForward();}

                else
                { 
                    Toast toast=Toast.makeText(getApplicationContext(), "Não há paginas para avançar", Toast.LENGTH_SHORT);
                    toast.show();   

                }}});


    Ir.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v)
            {

                pagina.loadUrl(Barra.getText().toString());}});


    voltar.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                if (pagina.canGoBack())
                {
                    pagina.goBack();}
                else
                { 
                    Toast toast=Toast.makeText(getApplicationContext(), "Não há paginas para voltar", Toast.LENGTH_SHORT);
                    toast.show();
                }}});       
    reload.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v)
            {
                pagina.reload();
                    };});}}
  • Jefferson, it could be something on the OR webview. I advise you to embed a browser in your application so that the behavior is always the same regardless of the android version. Look at the link of this project I use it a lot and breaks a branch https://crosswalk-project.org/

  • Thanks for the advice Hiago.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.