YouTube no WebView for Android app

Does anyone know how to view a YouTube video or some 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 Video NasaTV, but it is only loading. I also tried to put a YouTube link and it does not run through 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.br");
}

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 manifesto.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>
Author: viana, 2015-01-01

1 answers

Good Night Luiz!

Instead of using the webview to view the videos, use a VideoView. Use webview only to view pages.

Then add a VideoView and follow the example code below:

    VideoView video = (VideoView) findViewById(R.id.myVideo);
    String vidAddress = "http://r3---sn-oxupm-nv4e.googlevideo.com/videoplayback?signature=BC3A63220C594522C360CB7486FC1E8D12DCDAB8.CAC1407F3FE7F20EFCAA52A0CAE0CF326F9ED28F&expire=1420274975&mv=m&initcwndbps=1832500&mt=1420253339&ms=au&ipbits=0&itag=18&mm=31&id=o-ANfN_ioVopr0rRldVzihGYKSVBcW128i3qCtQdgFUZD1&ip=82.118.249.190&key=yt5&source=youtube&sver=3&dur=546.597&fexp=900718%2C916644%2C927622%2C932404%2C933112%2C9406025%2C941004%2C942632%2C943917%2C947209%2C947218%2C948124%2C952302%2C952605%2C952901%2C955105%2C955301%2C957103%2C957105%2C957201&ratebypass=yes&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmm%2Cms%2Cmv%2Cratebypass%2Csource%2Cupn%2Cexpire&upn=hpPTxmA5wT8&title=DevBytes-+Criando+seu+primeiro+projeto+no+Android+Studio";
    Uri vidUri = Uri.parse(vidAddress);
    video.setVideoURI(vidUri);
    video.start();

Note: this gigantic link is from a youtube video with the link already converted for direct viewing.

Test there and let me know. Here it worked perfectly. Hug.

 2
Author: MagnusMaker, 2015-01-03 02:56:14