Advertisement
ali50mahmoud

andro-youtube_api

Aug 21st, 2017
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. Hi , In this tutorial I am going to explain How to play youtube video in any Android application. We are going to use youtube API to play youtube video within a Android application. Youtube API required minimum Android version 4.2 .We can see lots of android apps playing youtube video inside the App to tell the app intro . This app will have a single screen with a video playing in it. This article covers basics of YouTube Android API. If you want to build a fully fledged youtube app, please go through Youtube Android Player API docs provided by Google.
  2.  
  3. Read more
  4. How to play Video streaming Application in Android
  5. How to parse Json Data in Android Application
  6. How to Create PDF reader Application in Android
  7. How to create your first Android Game
  8. Prerequisites:
  9. JDK 7.0 or Above
  10. Android Studio 2.0
  11.  
  12. Steps to follow:
  13. Step 1: Create a New Android Studio project name YoutubePlayer.
  14.  
  15. Step 2: Select minimum API level 17 so that it can run Maximum of the Android device available in playstore.
  16.  
  17. Step 3: Give your layout xml name is activity_main And click finish .
  18.  
  19. Step 4: Login to API console
  20.  
  21. Step 5: Create a Project -> Go to Services -> Turn on the switch for Google YouTube Data API v3
  22.  
  23. Step 6: Goto API Access -> Create new Android Key-> In popup enter one SHA1 certificate fingerprint and package name (separated by a semicolon) per line.
  24.  
  25. Step 7: To get SHA1 certificate of your app in Android studio open Terminal enter the following command keytool -list -v –keystore “path to key” . Then enter the password of your keystore . Scroll above to get SHA1 fingerprint key .
  26.  
  27. Step 8: From here copy SHA1 certificate and also copy package name of your Apps from Android Studio.
  28.  
  29. Step 9: Now enter these values in the API Console popup in the desired format, you key will be generated.
  30.  
  31. Step 10: Now Download Android YouTube Player API bundle from here. Unzip this file and follow the steps below to include it in your app:
  32. Copy the jar file from libs folder to the the libs folder of your project.
  33. Right click on your project, File -> Project Structure -> app -> dependencies -> add File dependencies
  34. Select YouTubeAndroidPlayerAPI.jar ->Ok
  35.  
  36. Step 11 : in manifest file add the internet permission
  37.  
  38. <uses-permission android:name="android.permission.INTERNET"/>
  39.  
  40. Step 12: Inside the activity_main.xml write the following code
  41.  
  42. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  43. xmlns:tools="http://schemas.android.com/tools"
  44. android:layout_width="match_parent"
  45. android:layout_height="match_parent"
  46. android:id ="@+id/play"
  47. tools:context="com.example.prosen.youtubeplayer.MainActivity">
  48.  
  49. <TextView
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:text="Hello World!"
  53. android:id="@+id/textView" />
  54.  
  55. <Button
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:text="Play"
  59. android:id="@+id/button1"
  60. android:layout_centerVertical="true"
  61. android:layout_centerHorizontal="true" />
  62.  
  63. <com.google.android.youtube.player.YouTubePlayerView
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:id="@+id/view1"
  67. android:layout_alignParentRight="true"
  68. android:layout_alignParentEnd="true"
  69. android:layout_above="@+id/button1"
  70. android:layout_alignParentLeft="true"
  71. android:layout_alignParentStart="true"
  72. android:layout_alignParentTop="true" />
  73.  
  74. </RelativeLayout>
  75.  
  76. Step 13 : Inside MainActivity.java write the following code
  77.  
  78. import android.support.v7.app.AppCompatActivity;
  79. import android.os.Bundle;
  80. import android.view.View;
  81. import android.widget.Button;
  82.  
  83. import com.google.android.youtube.player.YouTubeBaseActivity;
  84. import com.google.android.youtube.player.YouTubeInitializationResult;
  85. import com.google.android.youtube.player.YouTubePlayer;
  86. import com.google.android.youtube.player.YouTubePlayerView;
  87.  
  88. public class MainActivity extends YouTubeBaseActivity{
  89.  
  90. Button button;
  91. private YouTubePlayerView view;
  92. private YouTubePlayer.OnInitializedListener listener;
  93.  
  94. @Override
  95. protected void onCreate(Bundle savedInstanceState) {
  96. super.onCreate(savedInstanceState);
  97. setContentView(R.layout.activity_main);
  98.  
  99. view = (YouTubePlayerView)findViewById(R.id.view1)
  100.  
  101. listener = new YouTubePlayer.OnInitializedListener() {
  102. @Override
  103. public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
  104.  
  105. youTubePlayer.loadVideo("BvpqMiVNQ7s");
  106.  
  107. }
  108.  
  109. @Override
  110. public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
  111. button =(Button)findViewById(R.id.button1);
  112. button.setOnClickListener(new View.OnClickListener() {
  113. @Override
  114. public void onClick(View v) {
  115. view.initialize("AIzaSyAi_AF4xeTeo8Q_S0-3j0PFZWWLsycfzMs",listener);
  116. }
  117. });
  118. }
  119. };
  120. }
  121. }
  122.  
  123.  
  124. Now compile and test the Application in real device.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement