Not able to play videos (from youtube) in WebView
- by user1205193
I am using a webview to display a video (could be from youtube or vimeo) in my app. In order to not load the video webpages in the default Android Browser, I am also extending the WebViewClient so I can override the shouldOverrideUrlLoading method. This way the video webpage loads successfully in the WebView.
However, when I click on the embedded video on the WebView, it does not play. If I do not override the shouldOverrideUrlLoading method, and let the video webpages load in the default Android browser, the videos work just fine. Any ideas why the videos are not working in the WebView?
Also, the main reason why I overrode the shouldOverrideUrlLoading method is because if I do not do that, then when I exit the Android browser to come back to my activity (by hitting the back button on the phone), I see a white screen. Upon hitting the back button twice, I am able to get back to my Activity.
I am using the emulator to do this test. Here is my code:
public class YoutubeLink extends Activity {
WebView myWebView;
String video_url;
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.youtubelink);
//Retrieving data from ListSample.java
Bundle extras = getIntent().getExtras();
if(extras !=null) {
video_url = extras.getString("video_url");
Log.d("inside YoutubeLink.java", video_url);
}
myWebView = (WebView) findViewById(R.id.web);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new HelloWebViewClient());
myWebView.loadUrl(video_url); }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}}