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,481

dziner

New Member
Licensed User
Longtime User
YouTubeThumbnailView available?

Hi Erel,

thanks for your great work! I tried the YoutubeStandalonePlayer and it works great. Will there also be a YouTubeThumbnailView API available?
Or can I use Reflection? If so, could you please show me an example.

Thanks,
Edwin
 

socialnetis

Active Member
Licensed User
Longtime User
While a video in lightbox is playing, there is no chance to press any other button added to the activity, right?
Is there anyway to go from lightbox to fullscreen, while the video is playing? Because I see no button options in the lightbox mode
 

socialnetis

Active Member
Licensed User
Longtime User
Is it possible to interact with the youtube account of the user? Like bookmark as favorite or use the "watch later" option?
 

RedSun

Member
Licensed User
Longtime User
I have started writing an app using this library, but have come across a small problem.

My program is in forced portrait mode, ie.
B4X:
#SupportedOrientations: portrait

but I am trying to run the videos in fullscreen landscape, ie.
B4X:
YouTube.StartVideo("API", "VIDEO FILE", 0, True, False)

Now it sort of works, the issue being when the youtube api rotates the video into fullscreen mode and starts to play, the app restarts in the background. And when the video finishes and rotates the screen again to portrait, the program restarts again and throws up some errors.

Is anyone able to verify this issue, if so, is there a parameter to stop this restart?

It is not a huge issue as I can live with the lightbox mode as that works fine, I just thought it was worth asking if there was an easy fix.

Oh, and first post, thanks for a great program, I am loving it :)
 
Last edited:

socialnetis

Active Member
Licensed User
Longtime User
Hi, I have tested Erel's example with "SuportedOrientations: portrait" mode and the video in full screen and I don't get any errors.

When you have the phone in portrait, and want to play the video, your app goes landscape (I guess that the api does make some kind of exception on playing in fullscreen and having the portrait mode blocked), so according to the activity lifecycle, it goes to pause, create and resume (theregoes why your app restarts). And when the video stops, the app return to your activity on portrait mode, doing the create and resume again.

So you should make sure that your Sub Activity_Create is using the FirstTime parameter to initialize just one time the variables that need so, and don't loose the state when restarting the app. So basically you need to save the state in the pause Sub, and restore it in between Create and Resume
 

holdemadvantage

Active Member
Licensed User
Longtime User
Sorry i get black screen with Erel example 1.apk, also in a new app i wrote (with mu API KEY and playlist ID). My phone is samsung GS3, is there something worong in the code or Youtube change something? Thanks in advance
 

BowTieNeck

Member
Licensed User
Longtime User
Get and Set TimeMillis?

First, thanks for this library.

I'm calling StartVideo in Activity_Resume with AutoPlay set to True.

Can I get the value of TimeMillis in Activity_Pause so that, when the device is rotated, I can set this value in StartVideo and the video continues from where it was at the time of rotation?

(This would be similar to VideoView.Position from the Audio library)

Please ignore the above. I've realised that the YouTube video is playing in its own activity (which you mentioned earlier but I hadn't realised the consequences) and so the Activity_Pause isn't triggered.
 
Last edited:

AlbertLee

Member
Licensed User
Longtime User
Hi Erel,

After downloading the YouTube Android Player API and placing the YouTubeAndroidPlayerAPI.jar in my Libraries folder, I could not find any instance of this library on the Libs tab in the B4A IDE.

Am I missing something here? The download did not include YouTubeAndroidPlayerAPI.xml, did this cause the missing library entry?
 

AlbertLee

Member
Licensed User
Longtime User
Hi Erel,

After downloading the YouTube Android Player API and placing the YouTubeAndroidPlayerAPI.jar in my Libraries folder, I could not find any instance of this library on the Libs tab in the B4A IDE.

Am I missing something here? The download did not include YouTubeAndroidPlayerAPI.xml, did this cause the missing library entry?
 

AlbertLee

Member
Licensed User
Longtime User
Thanks Socialnetis, if it's YouTube.zip your were referring to, then I had already downloaded that. I just could not seem to find the YouTubeAndroidPlayerAPI library on my Libs tab, the YouTube library from Erel was already on the tab...
 

socialnetis

Active Member
Licensed User
Longtime User
Then that's all you need, just put the Youtube.jar, Youtube.xml and YouTubeAndroidPlayerAPI.jar in the same folder. The b4a library call the YoutubeAndroidPlayerApi for you, you don't need to worry about it.
 

hanyelmehy

Active Member
Licensed User
Longtime User
youtube live stream

Code is working for youtube video ,but when try to use youtube Live stream ( i get error after application open youtube activity and try to open the stream
error :(there was a problem while playing and some time format is not supported)
any suggestions
 
Top