Saturday, August 2, 2014

Youtube Search and play Video in Windows Phone

Youtube Image

Now, Its Time for Searching the Videos in Youtube and play that Video in our Application so easily..!

Now Here is the Design (.xaml) file of my Youtube Search Application.
<phone:PhoneApplicationPage
x:Class="Nikhil'sYoutubeSearchApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="SearchTemplate">
<Grid Margin="0,0,0,30">
<TextBlock Text="{Binding Title}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="355" Margin="120,0,0,0" TextWrapping="Wrap" />
<Image Source="{Binding VideoImg}" HorizontalAlignment="Left" Width="100" Height="100" Stretch="UniformToFill"/>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBox Height="72" Name="TxtSearch" Text="" Width="460" SelectionBackground="#FF75A9C2" >
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF6E704D" Offset="0.884"/>
<GradientStop Color="#FFB0BA87"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
<Button Content="Button" HorizontalAlignment="Right" Height="72" Name="BtnSearch" Width="160" RenderTransformOrigin="0.5,0.5" Click="BtnSearch_Click">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FF113B60" Offset="1"/>
<GradientStop Color="#FF8DBFDA"/>
</LinearGradientBrush>
</Button.Background>
</Button>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox HorizontalAlignment="Left" Margin="0,0,0,0" Height="600" ItemTemplate="{StaticResource SearchTemplate}" ItemsSource="{Binding SearchResults}" x:Name="LstVideobox" VerticalAlignment="Top" Width="460" SelectionChanged="LstVideobox_SelectionChanged">
<ListBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF413D31"/>
<GradientStop Color="#FF54564F" Offset="0.457"/>
</LinearGradientBrush>
</ListBox.Background>
</ListBox>
</Grid>
</Grid>

</phone:PhoneApplicationPage>


And Now the Code File for that is.. (xaml.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using Microsoft.Phone.Controls;

namespace Nikhils_YoutubeSearch_App
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void BtnSearch_Click(object sender, RoutedEventArgs e)
    {
        var wc = new WebClient();
        wc.DownloadStringCompleted += DownloadStringCompleted;
        var src = string.Format("http://gdata.youtube.com/feeds/api/videos?q={0}&format=6", HttpUtility.UrlEncode(TxtSearch.Text));
        wc.DownloadStringAsync(new Uri(src));
    }

    void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        var atomns = XNamespace.Get("http://www.w3.org/2005/Atom");
        var medians = XNamespace.Get("http://search.yahoo.com/mrss/");
        var xml = XElement.Parse(e.Result);
        var videos = (from entry in xml.Descendants(atomns.GetName("entry"))
        select new VideoClass
            {
              VideoID = entry.Element(atomns.GetName("id")).Value,
              VideoImg = (from thumbnail in entry.Descendants(medians.GetName("thumbnail"))
            where thumbnail.Attribute("height").Value == "360"
            select thumbnail.Attribute("url").Value).FirstOrDefault(),
            Title = entry.Element(atomns.GetName("title")).Value
            }).ToArray();
        LstVideobox.ItemsSource = videos;
    }

    private void LstVideobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var curvid = LstVideobox.SelectedItem as VideoClass;
        if (curvid != null)
        {
            var parsedvid = curvid.VideoID.Split('/');
            var id = parsedvid[parsedvid.Length - 1];
            var url = "vnd.youtube:" + id;

            var myfinalurl = "https://www.youtube.com/embed/" + id;

            var tempurl = "https://www.youtube.com/watch?v=" + id + "?autoplay=1";

            var task = new WebBrowserTask { URL = "https://www.youtube.com/embed/" + id + "?autoplay=1" };
            task.Show();
        }
    }
}
}



Now, Finally you created a Application of Youtube Searching and Playing that video.,,!!
:)