Youtube on Webview for Android app

Asked

Viewed 1,656 times

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).

1 answer

2

Good night Luiz!

Instead of using webview to view videos, use a Videoview. Use the 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.

  • Sorry Magnusmaker, but I need to comment, this link usually changes, not recommended, the structures of "stream", his problem is something else.

  • Thanks for the warning, I really didn’t know that. But it’s a method that works for permanent links too (if you have a fixed .mp4 link for example). Anyway, if I got it wrong please disregard then.

  • Yes to fixed links works perfectly and so there will be no need to exit the application as well as the official Youtube application works. At first we can say that until his solution works, but if he wants to develop a Webbrowser, then still the problem will exist, since it occurs with two different video sites, then the problem would still exist. I hope you’ll take this as a constructive comment.

  • Quiet.. I’m here to learn above all and any correction is welcome.

  • Thank you so much for the intention to help!! In fact I need streaming to run through Videoview but I am aware that this is not good practice. Unlike iOS, Android has these peculiarities. But still, if you know something you can add, I’ll be very grateful. Hugs. @Guilherme Nascimento

  • Not that @Luizhenriqueugliano believe your app even works, the problem should be something I mentioned in my first comment, the page should be trying to load the player in Flash instead of Html5 (tag <video>).

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.