Android Example Youtube playlist with xml2map

Almora

Active Member
Licensed User
hi..
I prepared the youtube playlist using the examples in the forum.

Used libraries:
➡ Javaobject
➡ Labelextras
➡ Okhttputils2
➡ Snyoutube
➡ Stringutils
➡ Xcustomlistview
➡ Xml2map
➡ Xmlbuilder
➡ Xmlsax
➡ Xui

Snyoutube library is used to play the video. If you want to use Youtube library.


B4x playlist url:
https://www.youtube.com/playlist?list=PLSXGv13TotQZUPJCw1lnxeaPsfYjns5W-


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
  
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
  
  
    Private ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
  
 
    Activity.LoadLayout("main")

  
    ListView1.AddSingleLine("B4X Video")
 

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

 

Sub ListView1_ItemClick (Position As Int, Value As Object)
    StartActivity(b4x)
End Sub

B4X:
Sub Process_Globals
    Type RssCardData (Title As String, Content As String, Link As String, url As String ,   pubDate As String)
    Private xui As XUI
 
End Sub

Sub Globals
  
    Private CLV1 As CustomListView
    Private lblTitle As B4XView
    Private lblContent As B4XView
    Private playbutton As Label
    Private sharebutton As Label
  
    Private web As WebView
  
  
End Sub


Sub Activity_Create(FirstTime As Boolean)
  
    Activity.LoadLayout("1")
 
    youtubelist
  
 
End Sub



Sub youtubelist
    ProgressDialogShow("loading...")
    Dim j As HttpJob
    j.Initialize("job",Me)
  
 
    j.Download("https://www.youtube.com/feeds/videos.xml?playlist_id=PLSXGv13TotQZUPJCw1lnxeaPsfYjns5W-")    'use only the portion beginning with PL...
      
    
    Wait For (j) JobDone(j As HttpJob)
    ProgressDialogHide
    If j.Success Then
 
        ParseYoutube(j.GetString)
        Log(j.GetString)
    End If
    j.Release
  
End Sub


Sub ParseYoutube(rssString As String)
    Dim xm As Xml2Map
    xm.Initialize
    Dim ParsedData = xm.Parse(rssString) As Map
  
    Dim feed As Map = ParsedData.Get("feed")
    Dim entrys As List = feed.Get("entry")
    For Each entry As Map In entrys
        Dim title As String = entry.Get("title")
        Dim pubDate As String = entry.Get("published")
        Dim link As String = entry.Get("videoId")
        Dim group As Map = entry.Get("group")     
        Dim thumbnails As Map = group.Get("thumbnail")
        Dim Attributes As Map = thumbnails.Get("Attributes")
        Log($"Attributes: ${Attributes}"$)
        Dim url As String = Attributes.Get("url")
        Log($"url: ${url}"$)
        Log($"link: ${link}"$)
        Log($"entrys: ${entrys}"$)
        Log($"title: ${title}"$)
      
        AddCLVItem(title,link ,url, pubDate)
    Next
  
End Sub


Sub AddCLVItem(Title As String, Link As String,   url As String ,  pubDate As String)
    Dim cd As RssCardData
      
    cd.Initialize
    cd.Title=Title
    cd.Link=Link
  
    cd.url=url
    cd.pubDate=pubDate
    cd.Content=""
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, CLV1.AsView.Width, 400dip)

    p.LoadLayout("Card1")
    lblTitle.Text = cd.Title
 
    web.LoadHtml($"<img src="${url}" style= "width:100%"/>"$)
 
    Dim htm As Html
    Dim su As StringUtils
      
    lblContent.Text = htm.FromHtml(cd.Content)
    lblContent.Height=su.MeasureMultilineTextHeight(lblContent,lblContent.Text)
    playbutton.Top=lblContent.Height+lblContent.Top
    sharebutton.Top=lblContent.Height+lblContent.Top
    p.Height=playbutton.Top+playbutton.Height+5dip
    CLV1.Add(p,cd)


End Sub



Sub playbutton_Click
    Dim index As Int = CLV1.GetItemFromView(Sender)
    Dim cd=CLV1.GetValue(index) As RssCardData
    CallSubDelayed2(play,"Show",cd.Link)
  
    Log($"Action 1 clicked. Index: ${index}"$)
End Sub



Sub sharebutton_Click
 
End Sub



Sub Activity_Resume
 
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


B4X:
Sub Process_Globals
 
    Dim videoid As String
  
End Sub

Sub Globals
 
      
    Dim Youtube As SNYouTube
    Dim Panel1 As Panel
    Dim apikey As String="XXX"
  
End Sub

Sub Activity_Create(FirstTime As Boolean)
 
     Activity.LoadLayout("play")
  
  
    Youtube.Initialize("Youtube",apikey,Panel1)
    ProgressDialogShow("loading...")
  
End Sub




Sub Activity_Resume

 
End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
End Sub



Sub Show(URL As String )
    videoid=URL
 
End Sub



Sub Youtube_OnFinishedInitialization(Success As Boolean)
  
    Log("Success: " & Success)
  
    If Success   Then
      
        ProgressDialogHide
        Youtube.cueVideo(videoid)
  
          
     End If

End Sub
 

Attachments

Last edited:
Top