Affichage des articles dont le libellé est UI. Afficher tous les articles
Affichage des articles dont le libellé est UI. Afficher tous les articles

How to play YouTube video in Android application ?


As we know playing a video stored in user device will never be a solutions specially if you app contains a large amount of information’s like a city guide, e-learning app... and so on and storing it locally we surely abuse the user and push him to uninstalled. So in this tutorial we are going to play a YouTube video with tow different methods and we are going also to play a video from an SDCARD as a demonstration of the Video View component

 Very important playing a video with android emulator will never work properly you must use a real android device for testing This method will launch the YouTube app and not in a videoview All you have to do is to paste this code inside the event of your button and don't forget to replace video_id by the located on the URL of YouTube video you want to play

String video_id = "2vFLW-idOxk";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + video_id);
startActivity(intent);
you can also use this method for clean and nice coding


private void startVideo(String videoID) {
 // default youtube app
 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + videoID));
 List list = getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
 if (list.size() == 0) {
  // default youtube app not present or doesn't conform to the standard we know
  // use our own activity
  i = new Intent(getApplicationContext(), YouTube.class);
  i.putExtra("VIDEO_ID", videoID);
 }
 startActivity(i);
}
Add the following permissions in the manifest file