Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

Windows Phone: listing and playing videos from YouTube (Visual Basic)

I am building an'app for Windows Phone 7.5 where I needed to show a list of videos from a YouTube channel, listing videos inside a ListBox and then allowing users to play the selected video.

I never did this before, so I found an interesting article on Visual Studio Magazine that put me on the right direction and that I adapted to my needs (not least writing code in Visual Basic ).

The first thing you need to do is constructing the Web address of the feed containing the desired list of videos. There is a number of URIs according to what you want to query, such as channels or categories. In this post I'm going to show you how to query a channel, which is nothing but querying the list of videos uploaded by a specific YouTube user.

YouTube provides specific APIs to do so. The full YouTube developer guide is available here.

Imagine you want to list all videos from the channel of Queen, my favorite rock band. The feed to retrieve the list is the following:

You can easily verify how it works by pasting the link inside your browser. How about using it in Windows Phone? First you need a DataTemplate for the ListBox. This DataTemplate is going to show the videos' thumbnail and description:

        <DataTemplate x:Key="SearchResultsItemTemplate">
            <StackPanel Margin="0,0,0,30" Orientation="Horizontal">
                <Image Source="{Binding VideoImageUrl}" HorizontalAlignment="Left" Width="100"   
         Height="90" Stretch="UniformToFill" VerticalAlignment="Top"/>
                <TextBlock Text="{Binding Title}" VerticalAlignment="Top" Width="300"
         HorizontalAlignment="Left" Margin="10,0,0,0" TextWrapping="Wrap"/>

Then you need a ListBox, which is simple:

                <ListBox x:Name="ResultsList" 
                         ItemTemplate="{StaticResource SearchResultsItemTemplate}" 
                         ItemsSource="{Binding}" SelectionChanged="VideoListSelectionChanged" />

As you can see the ListBox uses the previously defined template. Now there are three things to do. The first thing is defining a class that represents a video, in a simplified way:

Public Class YouTubeVideo
    Public Property Title As String
    Public Property VideoImageUrl As String
    Public Property VideoId As String
End Class

The second thing to do is implementing the load operation for the list of videos. The place where you will write the code depends on your app implementation, however this is what you will write:

Dim w As New WebClient
AddHandler w.DownloadStringCompleted, Sub(sender As Object, e As DownloadStringCompletedEventArgs)
Dim atomns = XNamespace.Get("")
Dim medians = XNamespace.Get("")
Dim xml = XElement.Parse(e.Result)
Dim videos = (From entry In xml.Descendants(atomns.GetName("entry"))
              Select New YouTubeVideo With {.VideoId = entry.Element(atomns.GetName("id")).Value,
              .VideoImageUrl = (From thumbnail In entry.Descendants(medians.GetName("thumbnail"))
              Where thumbnail.Attribute("height").Value = "90"
              Select thumbnail.Attribute("url").Value).FirstOrDefault(),
              .Title = entry.Element(atomns.GetName("content")).Value}).ToArray()
                                                        ResultsList.ItemsSource = videos
searchUri = ""
w.DownloadStringAsync(New Uri(searchUri))

We can use LINQ to XML to parse the content of the feed and create an instance of the YouTube class which is populated with values coming from the feed. I'm not using XML literals here because we are referring to a namespace which is different in the Descendants element.

The very last step is handling the SelectionChanged event on the ListBox:

    Private Sub VideoListSelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
        Dim video = CType(ResultsList.SelectedItem, YouTubeVideo)
        If video IsNot Nothing Then
            Dim parsed = video.VideoId.Split(CChar("/"))
            Dim id = parsed(parsed.Length - 1)
            Dim playbackUrl = "" + id
            Dim task As New WebBrowserTask With {.Uri = New Uri(playbackUrl)}
        End If
    End Sub

Basically the selected item is converted into an instance of the YouTubeVideo class; properties of the instance are read and these are used to construct the video's URL which is then launched via the WebBrowserTask launcher.

At this point you should not be suprised that your application will try to launch the YouTube app, which is created from Microsoft and that is free. The particular video format requires such an app to be available. This implies that you will not be able to test the code on the Windows Phone Emulator, because this does not allow downloading apps from the MarketPlace. This definitely means that you will have to test the result on the physical device.

If you disregard this limitation, you will see how effective this technique is especially if you need to play videos that are not available in other formats.


Print | posted on sabato 21 gennaio 2012 00:04 | Filed Under [ Visual Basic Silverlight/Windows Phone ]


No comments posted yet.

Post Comment

Please add 5 and 6 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET