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

socialnetis

Active Member
Licensed User
Longtime User
Erel, do you think it would be possible to wrap the YouTubePlayerView? I've tried but the activity in which you want to use the view needs to extend the YouTubeBaseActivity.
 

Buks

Member
Licensed User
Longtime User
I get an error:
Compiling generated Java code. 0.47
Convert byte code - optimized dex. Error
A referenced library is missing: youtubeandroidplayerapi
 

Vincenzo Fabiano

Member
Licensed User
Longtime User
When i try to play some Video, Youtube app crash! Why? With other video i have no problem.. Sorry for my bad english.

Video Problem = ?v=SOeOPlsXh3s

Test Phone = Galaxy s4 and Sony Miro.


EDIT: App no more crashes but YouTube report: "Si è verificato un errore con la rete (500)"
 
Last edited:

Vincenzo Fabiano

Member
Licensed User
Longtime User
Sorry Erel, problem is solved! Google change our services (api key), and YouTube Api were disabled! (Service now is called Google Cloude)
 

Ratna Fang

Member
Licensed User
Longtime User
hi erel,
is it possible to retrieve image thumbnail, title, and video ID from a given youtube playlist ID?

i'm searching the way to make a listview based on a PlaylistID
 

Ratna Fang

Member
Licensed User
Longtime User
thanks alot, erel :)
i'll learn more from your example, imagedownloader or flickrviewer
 

Shahid Saeed

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

I am in a similar situation, I am using a splash screen, and app is setup for Full Screen portrait mode, when you play a video it opens in landscape full screen and when quit video the APP restarts and this time stays at splash screen.

I have tried to handle through pause sub, resume sub but no luck; Here is the code for your reference:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   
    pnllist.Initialize
    BayanCList.Initialize
   
    If FirstTime Then   
    isFirst = True   
    End If
   
    SplashTimer.Initialize("SplashTimer", 4000)
    SplashTimer.Enabled = True
    Activity.LoadLayout("spalsh")
   
End Sub
Sub Activity_Resume   
    If isFirst = False Then   
        Dim pnl As Panel = BayanList.GetPanel(posBayan-1)
        If pnllist.Size > 0 Then
        For i = 0 To pnllist.Size-1
            Dim p As Panel = pnllist.Get(i)
            p.Color = Colors.Transparent
        Next
        End If
        pnl.Color = pnlBG       
    End If   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True Then
    Activity.Finish
    End If
    posBayan = posBayan
    isFirst = False    
End Sub

Sub SplashTimer_Tick   
    SplashTimer.Enabled = False
    DoEvents
    Activity.RemoveAllViews
    DoEvents
    Activity.LoadLayout("Main")
End Sub
 

Shahid Saeed

Active Member
Licensed User
Longtime User
can i view youtube in fullscreen??
Yes, you can view YouTube in full screen mode.

Full Screen view
B4X:
Sub PlayVideo_Click
 YouTube.StartVideo("AIzaSyxxxxxxxx-xxxx-xxxxxxxxxxxx", _"0xdZXOM_Otc", 0, True, True)
End Sub

Lightbox Portrait view
B4X:
Sub PlayVideo_Click
 YouTube.StartVideo("AIzaSyxxxxxxxx-xxxx-xxxxxxxxxxxx", _"0xdZXOM_Otc", 0, False, True)
End Sub
 
Top