Share My Creation List Youtube videos and play them full screen in a Webview!

Syd Wright

Well-Known Member
Licensed User
After years of messing about with all sorts of (half-baked) Youtube player solutions, I have finally found a good solution to create a list of Youtube videos based on a search criterium and to also automatically play any selected video full screen in an ordinary Webview! No need to use the Youtube API, nor the quirky Youtube player module which also runs out of API credits after using it too long, nor the need for separate activities, nor intents!
In landscape mode the Webview is enlarged in order to show the video part of the Youtube browser screen in full screen. The reason is that Youtube has blocked full screen ability in a Webview.

I spent hours on trying to make the videos auto-play and did find various scripts that do the trick, but all are based on embedding Youtube video's. This has the nasty side effect of refusing to play about half of all videos because the uploader has forbidden embedding. I eventually solved it by simply simulating a screen touch event to make the video play as soon as the webview has loaded the Youtube webview screen. This is achieved with the Gesture Detector library and (touch and release) motion events.
The nice thing is that it is very easy to include this code in your own B4A project. Here is the code:

Youtube Full-screen auto-player:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: false
#End Region

Sub Process_Globals
    Dim GestDet1 As GestureDetector
End Sub

Sub Globals
    Dim IME As IME
    Dim Text1 As EditText
    Dim SourceWeb1 As String
    Dim SourceText1 As String
    Dim VSgrid1 As ListView
    Dim YouIDS1(100) As String
    Dim YouLink1 As String
    Dim WebView1 As WebView
    Dim WebViewSetting1 As WebViewSettings
End Sub

Sub Activity_Create(FirstTime As Boolean)
    IME.Initialize("IME")
    Text1.Initialize("Text1")
    VSgrid1.Initialize("VSgrid1")
    WebView1.Initialize("WebView1")
    Activity.AddView(Text1,5%x,3%y,90%x,15%y)
    Activity.AddView(VSgrid1,5%x,18%y,90%x,70%y)
    Activity.AddView(WebView1,0%x,0%y,100%x,100%y)

    WebView1.JavaScriptEnabled = True
    WebViewSetting1.setMediaPlaybackRequiresUserGesture(WebView1,False)
    WebView1.Visible = False

    Text1.TextSize = 60
    Text1.Text = "cliff richard"        'Create Songs index
    SourceWeb1 = "https://www.youtube.com/results?search_query=" & Text1.Text
    SourceWeb1 = SourceWeb1.Replace(" ","+")
    GET_YOUTUBE1
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode=4 Then
      If WebView1.Visible = True Then
        WebView1.Visible = False
        Return True                     'Consume event!
      End If
    End If
    Return False
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub GET_YOUTUBE1
    Dim n, m, i As Long
    Dim Titel1, YouID1 As String
    Dim J As HttpJob
    J.Initialize("", Me)
    J.Download(SourceWeb1)
    Sleep(500)

    Wait For (J) JobDone(J As HttpJob)
    If J.Success = False Then Return
      SourceText1 = J.GetString

    n=100
    i=0
    VSgrid1.Clear

    Do While n<SourceText1.Length-100
      n=SourceText1.IndexOf2("title" & Chr(34) & ":{" & Chr(34) & "runs",n) + 25          'title":{"runs":[{"text":"
      m=SourceText1.IndexOf2(Chr(34),n)
      If n<25 Or m<n Then Exit
      Titel1 = SourceText1.SubString2(n,m).Replace("\u0026","&")
      VSgrid1.AddSingleLine(Titel1)
      n=m+1
      n=SourceText1.IndexOf2("watchEndpoint" & Chr(34) & ":{" & Chr(34) & "videoId",n)+27   'watchEndpoint":{"videoId":"
      m=SourceText1.IndexOf2(Chr(34),n)
      If n<27 Or m<n Then Exit
      YouID1 = SourceText1.SubString2(n,m)
      YouIDS1(i)=YouID1
      i=i+1
      Log(Titel1 & " --> " & YouID1)
      n=m+1
    Loop
    J.release
End Sub

Sub WebView1_PageFinished (Url As String)         'Event
    If 100%x>100%y Then WebView1.SetLayout(-5%x,-5.5%y,150%x,150%y)   'Zooms the webview and hides the Youtube title bar and other Youtube browser elements
    If 100%x<100%y Then WebView1.SetLayout( 0%x,   0%y,100%x,100%y)   'Portrait mode

    WebView1.Visible = True
    WebView1.BringToFront
    Sleep(250)
    Dim MEv As Object = GestDet1.CreateMotionEvent(DateTime.Now, DateTime.Now, GestDet1.ACTION_DOWN, 50%x, 30%y)    'Simulate click event to start playing!
    GestDet1.PassTouchEventTo(MEv, WebView1)
    Dim MEv As Object = GestDet1.CreateMotionEvent(DateTime.Now, DateTime.Now, GestDet1.ACTION_UP,   50%x, 30%y)
    GestDet1.PassTouchEventTo(MEv, WebView1)
End Sub

Sub VSGrid1_ItemClick (Position As Int, Value As Object)
    YouLink1 = YouIDS1(Position)
    Log(YouLink1)
    SourceWeb1 = "https://www.youtube.com/watch?v=" & YouLink1
    WebView1.LoadUrl(SourceWeb1)
End Sub

Sub Text1_TextChanged(Old As String,New As String)
    If New.Contains(CRLF) Then
      SourceWeb1 = "https://www.youtube.com/results?search_query=" & New.Replace(CRLF,"")
      SourceWeb1 = SourceWeb1.Replace(" ","+")
      IME.HideKeyboard
      GET_YOUTUBE1   
      Text1.Text = Old
    End If
End Sub
 
Last edited:
Top