B4A Library YouTube library

The YouTube library allows you to play YouTube videos inside your application.

It is based on a new service provided by Google. Note that this service is considered experimental.

This library is supported by Android 2.2+. It requires YouTube app v4.2.16 which is currently the latest version. You can update YouTube from Google Play.


Setup instructions
1. Download the native library: https://developers.google.com/youtube/android/player/downloads/
2. Copy libs\YouTubeAndroidPlayerApi.Jar from the zip file to Basic4android libraries folder.
3. Download the attached library, unzip it and copy the files to the libraries folder.
4. Get a developer key. Follow these instructions: https://developers.google.com/youtube/android/player/register
Note that you can leave the SHA1 field empty.

Example code:
Sub Process_Globals

End Sub

Sub Globals
   Dim YouTube As YouTubeStandalonePlayer
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If YouTube.CheckPlayerSupported <> "SUCCESS" Then
      Msgbox("YouTube service not available: " & CRLF & YouTube.CheckPlayerSupported, "")
      Activity.AddMenuItem3("Play Video", "PlayVideo", Null, True)
   End If
End Sub
Sub PlayVideo_Click
   YouTube.StartVideo("AIzaSyxxxxxxxx-xxxx-xxxxxxxxxxxx", _
      "0xdZXOM_Otc", 0, False, True)
End Sub

Sub YouTube_Result (Message As String)
   Log("Result = " & Message)
End Sub

Important, YouTube service will not work if the Version Name field is empty.

The player can play in full screen or in lightbox mode. The above screenshot is for lightbox mode.


  • YouTube.zip
    3.5 KB · Views: 2,047

Henry Baars

Licensed User
When I try the attached code on my my tablet (acer with android 4.1) "the program has unfortunately stopped"...
YouTube.Initialize("YouTube") seems to work. But when I enable YouTube.CheckPlayerSupported
The app crashes.


(debug dump)
java.lang.NoClassDefFoundError: com.google.android.youtube.player.YouTubeApiServiceUtil
at anywheresoftware.b4a.objects.YouTubeStandalonePlayerWrapper.CheckPlayerSupported(YouTubeStandalonePlayerWrapper.java:36)
at b4a.Scrub.main._activity_create(main.java:302)


Acer tablet (512MB) Android 4.1
Youtube app version 11.23.56

Any tips, tricks on how to overcome this problem?
What could cause the crash?



  • b4avraag.txt
    820 bytes · Views: 62
Last edited:

Henry Baars

Licensed User
Hello Erel, thanks for your reply.

Yes, the google play services is running. (9.2.65) (with latest update in place as far as I can tell)
Maybe also worth mentioning. This is not my first android app. I have several other apps up and running. So the b4a combination with the acer tablet is working.
But I have been away from the keyboard for over a year now. Right now I am re-learning B4A again. So it is possible I overlooked something totally stupid.
But when I reinstall my other apps I created more then a year ago, they all install and work without any problems. This tells me the B4A environment (and libraries) have not changed that much over the last year. (I think)


  • full error message.txt
    1.2 KB · Views: 57

Henry Baars

Licensed User
Obviously not the right one...
Upgraded to 6.0.
Installing and running are ok now. (I had at least the Video ID and de API key things right. The youtube connection to a hidden link worked on the first run :)

So,... something totally stupid after all.
I totally forgot that B4A upgrades are not automatic. (I think?)
Anyways, thank you so much for pointing me in the right direction.

Rick Harris

Well-Known Member
Licensed User
Is it possible to change the size of the Youtube player (in Lightbox mode)?
In this version by Erel I did not find that possibility, but it is possible with:
However, this version (by "Socialnetis") won't play Youtube "Live" video, whereas Erel's version now does!

Would it also be possible to add an event to Erel's version that fires when the user taps the player screen (to show the controls)? I want the user to tap the screen to hide the player and bring back my own activity, instead of having to touch the Return button on the Android button bar.
Last edited:

Rick Harris

Well-Known Member
Licensed User
No. You don't have any control over the YouTube activity size or behavior.
Somehow Socialnetis has managed to change the Youtube player size in the thread that I mentioned:
It uses a Fragment Activity. With his solution, the size can be reduced to at minimum 200dip x 110dip (any smaller and the Youtube API refuses to play). Unfortunately it does not play "Live" Youtube streams (and sometimes throws a "YouTubeAndroidPlayerApi.jar" error), otherwise I would use his solution.


Licensed User
Hi ! There is no Close Button on Lightbox and FullScreen mode ! How user closes the video ?

And when i open in Full Screen mode, and press return button, my screen gets white !


Licensed User
The user can click on the back key.
Erel, thanks for reply ! But when back key is clicked, does not return to my activity and the screen gets white .... do i have do load all activity objects again on "Resume" sub ?

Douglas Farias

Licensed User
Hi all.
with this lib its possible get the YouTube_Result when the video its completed?
on the example code the video finish and the lightbox or fullscreen still opened, the YouTube_Result is called only when the user press backkey.
its possible detect when the video finish (complete) and call YouTube_Result or simulate a backkey press when video finish?



Well-Known Member
Licensed User
I am working with this amazing library, but i want integrate this with a sinage software that i developed.
how can i close the you tube session without interact with the screen?

any one with the same issue?



Licensed User
Hi Erel,
In the create a new key.. I found it one must upload (create) a project first in order to get a key.. but the project can not be created or tested without the key..
I know I am missing something so the question.. how this be solved?


Licensed User
I found it one must upload (create) a project first in order to get a key
Why? I can create 20 new keys without creating a new project.

  1. Go to the Google Developers Console.
  2. Select a project, or create a new one.
  3. In the sidebar on the left, expand APIs & auth. Next, click APIs. In the list of APIs, make sure the status is ON for the YouTube Data API v3.
  4. In the sidebar on the left, select Credentials.