How to put videos from youtube in Android application

1

I am working on an application where I want to show youtube videos in a fragment that has CardsViews, my cardviews already work without problem, the problem is that the videos are not reproduced, I always miss an error:

I am adding the videos in a WebView and the links of the videos I use them in the following way: link \ "+ yS_p_ICLUAw + \"? autoplay = 1 & vq = small

The configuration of the WebView is as follows:

holder.reproductor.loadUrl(tips.getEnlace());
        holder.reproductor.getSettings().setJavaScriptEnabled(true);
        holder.reproductor.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        holder.reproductor.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });

This is the error that shows with the URL that I put in the WebView:

    
asked by Enrique Espinosa 07.11.2018 в 16:03
source

2 answers

1

The problem is due to the definition of the url of the video, this is incorrect since it defines an incorrect url,

  

" link \" + yS_p_ICLUAw + \ "? autoplay = 1 & vq = small";

the url should not use " \ " since you would be escaping some characters ( \" ) , this is the correct definition of the url of the youtube video:

  

" link "

, review this example:

 String urlVideo = "http://www.youtube.com/embed/yS_p_ICLUAw?autoplay=1&vq=small";
 webView.loadUrl(urlVideo);

As a comment: I strongly advise the use of YouTube Android Player API

This is an example:

link

    
answered by 07.11.2018 / 18:58
source
0

instead of using a webview I recommend you use a MediaPlayer and pass the url of the video, something like this:

 MediaPlayer mp = new MediaPlayer();
 mp.setDataSource(url);
 mp.prepare();
 mp.start();

Also if you are going to use it in an adapter try to have only 1 MediaPlayer and not one for each card or it could cause problems.

    
answered by 07.11.2018 в 16:14