Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add YouTube Data API to Android Studio

Tags:

So, I am currently messing around with android programming in my free time, and I am using android studio as my ide of choice. I am currently trying to make a very simple app using the youtube api. My issue is that I cant figure out how to actually get the api into my application. I have used file>project structure to add the stuff to my application, but that doesnt work by itself. It stops yelling at me about syntax errors but when it compiles it errors.

I researched this a bit and have found that I need to add stuff to build.gradle or settings.gradle(or both) but i havent found a definitive answer on exactly what to do. Everything I have tried thus far hasnt worked. SO if someone could either explain to me what i have to do, or link me to a place where it explains what to do that would be great

--edit--

I am so confused right now. I started a new project. I added the libs and it was still able to compile. I then added the imports and it threw errors about it. I then added these to the build.gradle and it was able to compile perfectly

compile fileTree(dir: 'libs/youtube', include: '*.jar') compile fileTree(dir: 'libs/youtube/libs', include: '*.jar') 

But when i added this code that requires the library

/** Global instance of the HTTP transport. */ private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();  /** Global instance of the JSON factory. */ private static final JsonFactory JSON_FACTORY = new JacksonFactory();  /** Global instance of Youtube object to make all API requests. */ private static YouTube youtube; 

the syntax was fine but it threw an error upon compiling it

 Gradle: Execution failed for task ':Apitest:dexDebug'. > Failed to run command: 

and that was followed by about 100 lines of of file locations

Any ideas as to what i am doing wrong?

like image 737
Bigandrewgold Avatar asked Aug 11 '13 18:08

Bigandrewgold


People also ask

Can I use YouTube API in my app?

Can I use YouTube Android Player API in my App for free? Yes, you can! I have used it in two of my apps!


2 Answers

Just add this dependency in gradle file:

compile 'com.google.apis:google-api-services-youtube:v3-rev181-1.22.0' 

and use YouTube object.

reference: https://developers.google.com/api-client-library/java/apis/youtube/v3

like image 122
Evgeni Roitburg Avatar answered Nov 05 '22 06:11

Evgeni Roitburg


Step 1: Download YoutubeAndroidPlayerApi.jar from https://developers.google.com/youtube/android/player/downloads/

Step 2: Paste it in libs folder inside app folder of project

Step 3: Add following line to build.gradle in app folder of project:

compile files('libs/YouTubeAndroidPlayerApi.jar') 

Step 4:Add following imports to activity which extends YouTubeBaseActivity and implements YouTubePlayer.OnInitializedListener

import com.google.android.youtube.player.YouTubeBaseActivity; import com.google.android.youtube.player.YouTubeInitializationResult; import com.google.android.youtube.player.YouTubePlayer; import com.google.android.youtube.player.YouTubePlayerView; import com.google.android.youtube.player.YouTubePlayer.Provider; 

Step 5: Inside OnCreate method:

//Make sure you initialize youtube player  YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);  youTubeView.initialize(YoutubeAPIKey, this); 

Where YouTubeAPIKey you would get from google console after you register your project in web mode, dont select mobile domain like Android or any

Step 6: Override methods in interface implemented as:

@Override public void onInitializationFailure(Provider arg0,         YouTubeInitializationResult error) {     // TODO Auto-generated method stub     Toast.makeText(this, "Oh no!           "+error.toString(),Toast.LENGTH_LONG).show();  }  @Override public void onInitializationSuccess(Provider arg0, YouTubePlayer player,         boolean arg2) {     // TODO Auto-generated method stub     player.loadVideo(VIDEO_ID); } 

Where VIDEO_ID is fetched from RESTful call to Youtube API at: "https://www.googleapis.com/youtube/v3/search" with suitable parameters passed in GET like, "q,relevanceLanguage,type,key" etc.

like image 37
Anand Kamathi Avatar answered Nov 05 '22 04:11

Anand Kamathi