B4A Library YouTube library

Erel

Administrator
Staff member
Licensed User
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:
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

Theera

Well-Known Member
Licensed User
Hi Erel,
I tried follow as your guide,but I have some error as belows. Please help me corrected

Best Regards
Theera
 
Last edited:

Inman

Well-Known Member
Licensed User
Thanks Erel. That was fast! Tested on my Asus Transformer tablet running Android 4.0 and it works great.

One issue I noticed is an error at the time of compilation saying there is a duplicate refresh.png entry. I have a refresh.png file already in my app. Do you have a same name file in the YouTube library as well?
 

hasanaydin52

Member
Licensed User
Hello Erel,

I get an error as above.

** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
Result = INTERNAL_ERROR
** Activity (main) Resume **
 

hasanaydin52

Member
Licensed User
Unfortunately does not response.

Log messages: 10...9...8..7.... .. ....0

My phone is Galaxy S2. Android version is 4.1.2 .
 

Inman

Well-Known Member
Licensed User
Hello Erel,

I get an error as above.

** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
Result = INTERNAL_ERROR
** Activity (main) Resume **
I got this error when I didn't set the Version Name and Version Code of the app. Once I did that, it worked flawlessly.
 

hasanaydin52

Member
Licensed User
Hello Erel,

Inman is right. It is worked when define the VersionName.

I did not know how important this is.

Thanks for library.
 

hasanaydin52

Member
Licensed User
Hello Erel,

StartVideo function running good but i can't run StartPlaylist.


B4X:
YouTube.StartPlaylist("AIza....","ballislife",0,1000,True,True)
Result 404 Network Error....
 

Erel

Administrator
Staff member
Licensed User
Did my example (APK file) work or not?

Make sure that you are using the correct ID.
 

hasanaydin52

Member
Licensed User
Sorry I understand as Developer Key ID.

I used ID as "ballislife" in function but does not run.
I dont know is "ballislife" true ID.

Can you send me another sample ID?

B4X:
YouTube.StartPlaylist("AIza...","ballislife",0,0,False,False)
 
Last edited:

AndroIwe

Member
Licensed User
Thank you for this example.

I want to load the video on a specific position and tried it with "Activity.AddView":

B4X:
Sub Globals
    Dim YouTube As YouTubeStandalonePlayer
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("YouTubeTest1")
   YouTube.Initialize("YouTube")
    Activity.AddView(YouTube, 10dip, 10dip, 250dip, 400dip)   
   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("AIzaSyDhjNZFx50axXbAxxxxx", _
        "kHuSNntPdA8", 0, True, True)
End Sub
But I got an error:
Activity.AddView(YouTube, 10dip, 10dip, 250dip, 400dip)
javac 1.7.0_07
src\Iwe\YouTube\Test\main.java:238: error: inconvertible types
mostCurrent._activity.AddView((android.view.View)(mostCurrent._v5),anywheresoftware.b4a.keywords.Common.DipToCurrent((int)(10)),anywheresoftware.b4a.keywords.Common.DipToCurrent((int)(10)),anywheresoftware.b4a.keywords.Common.DipToCurrent((int)(250)),anywheresoftware.b4a.keywords.Common.DipToCurrent((int)(400)));
^
required: View
found: YouTubeStandalonePlayerWrapper
Is there any way to place the video on a specific position?
 
Top