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.

SS-2013-01-03_11.57.47.png


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:
B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim YouTube As YouTubeStandalonePlayer
End Sub

Sub Activity_Create(FirstTime As Boolean)
   YouTube.Initialize("YouTube")
   If YouTube.CheckPlayerSupported <> "SUCCESS" Then
      Msgbox("YouTube service not available: " & CRLF & YouTube.CheckPlayerSupported, "")
   Else
      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.
 

Attachments

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

wimpie3

Well-Known Member
Licensed User
Longtime User
I suppose you can only use this youtube library to play ONLINE videos, and not videos I have stored on the internal memory?
 

eSolution

Member
Licensed User
Longtime User
I have used this lib in 3 of my apps to play inapp youtube videos and Google had no problem with it till last month... Last month I uploaded an app made using the same "template" as in my last 3 apps and the app was rejected twice with this message: "
REASON FOR REMOVAL: Violation of section 4.4 of the Developer Distribution Agreement.

After a regular review, we have determined that your app downloads, monetizes, or otherwise accesses YouTube videos in violation of the YouTube Terms of Service or YouTube API Terms of Service. Accessing content, a product, or service in an unauthorized manner is a violation of the Developer Distribution Agreement, and is not allowed on Google Play."

... Is there a problem with the API? Is Google forcing us to use only the official youtube app from now on? There are only 2 possible reasons for this rejection : I have ads on the app page that show the lightboxed youtube video and they don't let me "monetize" a yt video outside the YT app or there is a problem with the way the library access youtube (may be something new in the yt TOS?? ) ... I would like to test the app without ads and see if is rejected again but I'm afraid for a rejection, third time in a row, I don't want my dev account to be affected ...

Anyone know a reason for this kind of rejection? There is some kind of problem with this lib in the last 2 month??

P.S. If I make the app to play the videos in full screen (not in lightbox) then when the video is finished and the user hit the back button (the mobiles back button, as there is no other button on that page in full screen mode) the activity (and the video) will restart ... That will happen every time I try to go back to in the app to the last activity ... :| ... so I think full screen is not an option, only lightbox version
 
Last edited:

Shahid Saeed

Active Member
Licensed User
Longtime User
I have used this lib in 3 of my apps to play inapp youtube videos and Google had no problem with it till last month... Last month I uploaded an app made using the same "template" as in my last 3 apps and the app was rejected twice with this message: "
REASON FOR REMOVAL: Violation of section 4.4 of the Developer Distribution Agreement.

After a regular review, we have determined that your app downloads, monetizes, or otherwise accesses YouTube videos in violation of the YouTube Terms of Service or YouTube API Terms of Service. Accessing content, a product, or service in an unauthorized manner is a violation of the Developer Distribution Agreement, and is not allowed on Google Play."

... Is there a problem with the API? Is Google forcing us to use only the official youtube app from now on? There are only 2 possible reasons for this rejection : I have ads on the app page that show the lightboxed youtube video and they don't let me "monetize" a yt video outside the YT app or there is a problem with the way the library access youtube (may be something new in the yt TOS?? ) ... I would like to test the app without ads and see if is rejected again but I'm afraid for a rejection, third time in a row, I don't want my dev account to be affected ...

Anyone know a reason for this kind of rejection? There is some kind of problem with this lib in the last 2 month??

P.S. If I make the app to play the videos in full screen (not in lightbox) then when the video is finished and the user hit the back button (the mobiles back button, as there is no other button on that page in full screen mode) the activity (and the video) will restart ... That will happen every time I try to go back to in the app to the last activity ... :| ... so I think full screen is not an option, only lightbox version
I guess the reason might be the ads, because YouTube has already started ads on mobile and TV devices, and there might be a limit on displaying ads other than YouTube player, because on websites Google has already limited repeatability of ads and they used to block ads if they found multiple ad instances on same page. I think you must try the app without ads to confirm the exact reason.
 

eSolution

Member
Licensed User
Longtime User
I guess the reason might be the ads, because YouTube has already started ads on mobile and TV devices, and there might be a limit on displaying ads other than YouTube player, because on websites Google has already limited repeatability of ads and they used to block ads if they found multiple ad instances on same page. I think you must try the app without ads to confirm the exact reason.

Thx bro! I already uploaded a new version of the app that uses intent to open the native youtube app in full screen mode so there is no add and it uses their app to play the videos and now is up and running on google play ... I will loose more then half of the potential earnings this way but... better then no earnings :D
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
Is it possible to include videos from youtube that are private? Maybe pass along a username / password to use these videos exclusively within your app?
 

Inman

Well-Known Member
Licensed User
Longtime User
Strange.

Could you please send me the project you tried so that I can see if it works for me as well?
 

Inman

Well-Known Member
Licensed User
Longtime User
I tried the same sample. When LightBoxMode=False the video just plays for a second and pauses. When you click the play button again, it restarts the playback for 1 second and again pauses.

What about you?
 

Inman

Well-Known Member
Licensed User
Longtime User
I tried setting Autoplay to True and False and in both cases it paused after initial playback
 

kozbot

Member
Licensed User
Longtime User
I'm having issues getting this to work. I'm not quite sure why... I keep getting the error:

Error occurred on line: 25 (Youtube)
java.lang.NullPointerException: Attempt to invoke virtual method 'void anywheresoftware.b4a.objects.YouTubeStandalonePlayerWrapper.Initialize(java.lang.String)' on a null object reference
at nesscorporation.apk.youtube._activity_create(youtube.java:478)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:305)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at nesscorporation.apk.youtube.afterFirstLayout(youtube.java:100)
at nesscorporation.apk.youtube.access$100(youtube.java:17)
at nesscorporation.apk.youtube$WaitForLayout.run(youtube.java:78)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
** Activity (youtube) Resume **

This is my code:

B4X:
Sub Globals 
    Dim YouTube As YouTubeStandalonePlayer
End Sub

Sub Activity_Create(FirstTime As Boolean)
   YouTube.Initialize("YouTube") ****This is the error line****
   If YouTube.CheckPlayerSupported <> "SUCCESS" Then
      Msgbox("YouTube service not available: " & CRLF & YouTube.CheckPlayerSupported, "")
   Else
      Activity.AddMenuItem3("Play Video", "PlayVideo", Null, True)
   End If
End Sub
Sub PlayVideo_Click
   YouTube.StartVideo("AIzaSyA-blahdyblahblahblah", _
      "sKJmy7wQhns", 0, True, True)
End Sub
Sub YouTube_Result (Message As String)
   Log("Result = " & Message)
End Sub

There's nothing different that I can see between the code Erel gave us and mine... am I missing something? I've got the libraries there, plus the api.jar, but I'm lost...

Cheers for the help!
 
Last edited:
Top