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 do not play, I always get an error:
I am adding the videos in a WebView and I use the video links as follows: http://www.youtube.com/embed/ \"+yS_p_ICLUAw+\"?autoplay=1&vq=small
The WebView configuration 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:
The problem is due to the definition of the video url, this is wrong as it defines a wrong url,
the url should not use "
\
" since you would be escaping some characters (\"
) , this is the correct definition of the youtube video url:, check this example:
As a comment: I strongly advise you to use the YouTube Android Player API
This is an example:
https://github.com/Jorgesys/Android-Youtube-API
Instead of using a webview, I recommend that you use a MediaPlayer and pass it the url of the video, something like this:
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.