Android Question youtube in listview or customlistview

doogal

Member
Licensed User
Longtime User
Hello all,
I have an idea for an app, but not familiar with youtube in B4A. Is it possible to list youtube videos in a listview or customlistview similiar to this example. These wouldn't be random videos. They would all be from the same youtube channel.

Thanks,
doogal
 

socialnetis

Active Member
Licensed User
Longtime User
Yes, its possible.
First you should get the youtube channel's videos. You can use httputils2 to download the channel's page, and then parse the content. The channels provide a little rss, that gives you the last 25 videos. You could also work with youtube lists (it can be a list from your youtube user, and it can contain all the videos of the users you want, but make sure its a public list), with this you don't have 25 limit videos as the channel's rss, but you will have to parse the html page as well.

Once you got everything parsed, you can populate the listview with, for example, the title of the videos, and when an item is clicked, you can start playing the video.
 
Upvote 0

doogal

Member
Licensed User
Longtime User
Thanks for the quick reply. I don't understand what you mean by list from youtube user though. HttpUtils2 is still new to me as I have never used it and haven't done any remote access programming. Can you suggest a tutorial for this or show a small example?
 
Upvote 0

socialnetis

Active Member
Licensed User
Longtime User
HttpUtils2 is a little library that offers an easy way to interact with web services (download pages, use POST methods,etc). You can read about this library HERE.

Once you catch up with the library you will see that is very easy to use.

Here is a little example:
Let's say you want to show in your listview, the lastest videos from the Smosh channel.
So, first you should download the channel's page: http://www.youtube.com/user/smosh
Once downloaded you will have an html page that needs to be parsed. This page is a little bit difficult to parse, so instead we will download the channel's feed: http://gdata.youtube.com/feeds/base...rderby=published&client=ytapi-youtube-profile
This is an XML file. XML are very easy to parse if you use the XML parser library (SaxParser).

So, here is a piece of working code, it will Log the ID, image link, and title of each video:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Parser As SaxParser
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("Layout1")
    Dim Job As HttpJob
    Job.Initialize("SmoshChannel",Me)
    Job.Download("http://gdata.youtube.com/feeds/base/users/smosh/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub JobDone(Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        If Job.JobName = "SmoshChannel" Then
            Log("Smosh Channel Downloaded")
            HandleSmoshChannel(Job)
        End If
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
    Job.Release
End Sub

Sub HandleSmoshChannel(Job As HttpJob)
    Log("Parsing the channel")
    Parser.Initialize
    Parser.Parse(Job.GetInputStream,"Parser")
End Sub

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
End Sub
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If Parser.Parents.IndexOf("item") > -1 Then
        If Name = "title" Then
            Log("Title: " & Text.ToString)
        Else If Name = "guid" Then
            Dim Image As String
            Dim Components() As String
            Image = Text.ToString
            Components = Regex.Split(":",Image)
            Image = "http://i.ytimg.com/vi/" & Components(3) & "/mqdefault.jpg"
            Log("Link id: " & Components(3))
            Log("ImageLink: " & Image)
        End If
    End If
End Sub

PS. And with list, I meant a playlist. For example: http://www.youtube.com/playlist?list=PL475D75813E9C76E5
 
Upvote 0

doogal

Member
Licensed User
Longtime User
This is exactly what I am wanting to do. This detailed howto will sure help. Thank you very much. I will try this out tomorrow its to late to try this now (2:30a).
 
Upvote 0

Abderrazzak

Member
Licensed User
Longtime User
hello
thank you for this post.
that work for me
can help to list the videos of a playlist youtube and not a channel ?

Regards
 
Upvote 0

socialnetis

Active Member
Licensed User
Longtime User
Hi Abderrazzak. Listing the videos of a playlist is a little bit more tricky because there is no feed. You have to parse an html page.

You will need a regular expression to catch the lines that contains relevant data. Here is a generic piece of code that should Log the Title, ID, and ImageLink of any Youtube Playlist (the example will download a Playlist from the Smosh channel, and Log some information):
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim Job As HttpJob
    Job.Initialize("YoutubeList",Me)
    Job.Download("http://www.youtube.com/playlist?list=PL475D75813E9C76E5")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub JobDone(Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        If Job.JobName = "YoutubeList" Then
            Log("Smosh List Downloaded")
            HandleYoutubeList(Job)
        End If
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
    Job.Release
End Sub

Sub HandleYoutubeList(Job As HttpJob)
    Dim TextReader1 As TextReader
    Dim Pattern, Line, LastLine As String
    Dim Link, Title, Image As String

    TextReader1.Initialize(Job.GetInputStream)
    Pattern = "v=" & "([^&]+)" & "[^""]+" & QUOTE & " title=" & QUOTE & "([^""]+)" & QUOTE & " class="

    Line = TextReader1.ReadLine
    LastLine = ""
    Do While Line <> Null
        Dim m As Matcher
        m = Regex.Matcher(Pattern,Line)
        If m.Find AND Not(m.Match.EqualsIgnoreCase(LastLine)) Then
            Link = m.Group(1)
            Title = m.Group(2)
            Image = "http://i.ytimg.com/vi/" & m.Group(1) & "/mqdefault.jpg"
            LastLine = m.Match
            Log("Title: " & Title & " -- Link: " & Link & " -- ImageLink: " & Image)
        End If
        Line = TextReader1.ReadLine
    Loop
End Sub
 
Upvote 0

Ratna Fang

Member
Licensed User
Longtime User
Hi Abderrazzak. Listing the videos of a playlist is a little bit more tricky because there is no feed. You have to parse an html page.

You will need a regular expression to catch the lines that contains relevant data. Here is a generic piece of code that should Log the Title, ID, and ImageLink of any Youtube Playlist (the example will download a Playlist from the Smosh channel, and Log some information):
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim Job As HttpJob
    Job.Initialize("YoutubeList",Me)
    Job.Download("http://www.youtube.com/playlist?list=PL475D75813E9C76E5")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub JobDone(Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        If Job.JobName = "YoutubeList" Then
            Log("Smosh List Downloaded")
            HandleYoutubeList(Job)
        End If
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
    Job.Release
End Sub

Sub HandleYoutubeList(Job As HttpJob)
    Dim TextReader1 As TextReader
    Dim Pattern, Line, LastLine As String
    Dim Link, Title, Image As String

    TextReader1.Initialize(Job.GetInputStream)
    Pattern = "v=" & "([^&]+)" & "[^""]+" & QUOTE & " title=" & QUOTE & "([^""]+)" & QUOTE & " class="

    Line = TextReader1.ReadLine
    LastLine = ""
    Do While Line <> Null
        Dim m As Matcher
        m = Regex.Matcher(Pattern,Line)
        If m.Find AND Not(m.Match.EqualsIgnoreCase(LastLine)) Then
            Link = m.Group(1)
            Title = m.Group(2)
            Image = "http://i.ytimg.com/vi/" & m.Group(1) & "/mqdefault.jpg"
            LastLine = m.Match
            Log("Title: " & Title & " -- Link: " & Link & " -- ImageLink: " & Image)
        End If
        Line = TextReader1.ReadLine
    Loop
End Sub

hi socialnetis,
i've been reading your code but i don't have any clue where i should put List1 to show the playlist result.

i think i can use your code and the put the result using List1.AddTwoLinesAndBitmap2
 
Upvote 0

socialnetis

Active Member
Licensed User
Longtime User
Hi Ratna, the code just Logs the Title, Video Id, and the ImageLink with this particular line:
Log("Title: " & Title & " -- Link: " & Link & " -- ImageLink: " & Image)

You can, instead, put the items in a ListView as you want. But you will need to download the image with the ImageLink by yourself (give a look to httputils2).
 
Upvote 0

Ratna Fang

Member
Licensed User
Longtime User
many thanks, socialnetis for figuring it out.

i'll add the list1.addtwolines first to go with the Link variable as value key to refer videoID and play it using youtube library.
 
Upvote 0
Top