9
Does anyone know how to view a Youtube video or any streaming video through a WebView
?
In the code below, there are three buttons. The first takes the user to the link he typed, the second is fixed to direct to the Google site and the third should run the streaming Nasatv video, but it’s just loading. I also tried to put a Youtube link and does not run through the WebView
.
My code:
public class MainActivity extends ActionBarActivity {
private EditText editText;
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.url);
webView = (WebView) findViewById(R.id.webViewlayout);
webView.setWebViewClient(new MyBrowser());
}
public void abrirPagina (View v){
String url = editText.getText().toString();
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(url);
}
public void acessoDireto (View v){
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl("http://www.google.com");
}
public void nasaTV (View v){
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl("http://www.ustream.tv/nasahdtv");
}
private class MyBrowser extends WebViewClient{
public boolean overrideUrlLoading (WebView view, String url){
view.loadUrl(url);
return true;
}
}
}
My manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.luizhmu.aulas_android_webview" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
As far as I understand, mobile browsers do not open the video directly, they call the video player app installed (and standard) of your smartphone, so if the video is only "loading" and never calls the mobile player app is why the displayed page is using a player on Flash (most smartphones do not work with Flash) or there’s something wrong with your video player on your smartphone. (Note: http page://m.ustream.tv/nasahdtv accessing by desktop simulators also does not load anything, but if you click on "videos" and select any video).
– Guilherme Nascimento