在Windows Phone 8中播放YouTube视频

时间:2014-08-22 12:09:02

标签: windows-phone-8

我已经解析了RSS feed http://www.teluguone.com/videosongs/videoSongsXml.php?cat_id=6并在水平滚动视图中显示歌词,如下所示。enter image description here

现在,当我点击特定的照片时,它应该在上面的空间中播放。

Xaml的代码是

<phone:PhoneApplicationPage
      x:Class="PhoneApp1.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"
      xmlns:delay="clr-namespace:Delay;assembly=PhonePerformance"
      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" Loaded="PhoneApplicationPage_Loaded">

<!--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">
        <TextBlock x:Name="ApplicationTitle" Text="Teluguone" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="Songs" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
                 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

           <MediaElement x:Name="player" AutoPlay="True" Margin="0,0,0,282"/>

        <ScrollViewer HorizontalScrollBarVisibility="Hidden" Height="Auto" Margin="0,411,0,63" >
            <ListBox x:Name="videosongList" ScrollViewer.HorizontalScrollBarVisibility="Auto"
            ScrollViewer.VerticalScrollBarVisibility="Disabled" Height="168" Width="412" SelectionChanged="videosongList_SelectionChanged" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,50,0,0"  Orientation="Horizontal">
                            <Image  x:Name="img1" Source="{Binding songpic}" ></Image>                                
                              <!--<TextBlock Text="{Binding songname}"  TextWrapping="Wrap"  VerticalAlignment="Bottom"
                                       FontSize="20" />-->                                
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemsPanel >
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"></StackPanel>                            
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>
        </ScrollViewer>
    </Grid>
</Grid>
 </phone:PhoneApplicationPage>

我在Xaml背后的代码是

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        Songsdetails song;

        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            // is there network connection available
            if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
            {
                MessageBox.Show("No network connection available!");
                return;
            }
            // start loading XML-data
            WebClient downloader = new WebClient();
            Uri uri = new Uri("http://www.teluguone.com/videosongs/videoSongsXml.php?cat_id=6", UriKind.Absolute);
            downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(VideosongsDownloaded);
            downloader.DownloadStringAsync(uri);
        }

        void VideosongsDownloaded(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null || e.Error != null)
                {
                    MessageBox.Show("There was an error downloading the XML-file!");
                }
                else
                {
                    // Deserialize if download succeeds
                    XDocument document = XDocument.Parse(e.Result);
                    XmlSerializer serializer = new XmlSerializer(typeof(videosongs));
                    videosongs Videosongs = (videosongs)serializer.Deserialize(document.CreateReader());
                   videosongList.ItemsSource = Videosongs.Collection;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.InnerException.Message);
                MessageBox.Show(ex.ToString());
            }
        }

        private void videosongList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var app = App.Current as App;
            app.selectedSongsdetails = (Songsdetails)videosongList.SelectedItem;

        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {

          var app = App.Current as App;
          song = app.selectedSongsdetails;


          var url = await YouTube.GetVideoUriAsync(song.songcode, YouTubeQuality.Quality480P);
           player.Source = url.Uri;
        }
    }
}

Songsdetails.cs

  public class Songsdetails
  {
       [XmlElement("songname")]
       public string songname { get; set; }

       [XmlElement("songpic")]
       public string songpic { get; set; }

       [XmlElement("songurl")]
       public string songurl { get; set; }

       [XmlElement("songcode")]
       public string songcode { get; set; }

       [XmlElement("songdescr")]
       public string songdescr { get; set; }

       [XmlElement("songtitletags")]
       public string songtitletags { get; set; }

       [XmlElement("songmetatags")]
       public string songmetatags { get; set; }

       [XmlElement("songmetadesc")]
       public string songmetadesc { get; set; }

       [XmlElement("songdate")]
       public string songdate { get; set; }
   }
}

Videosongs.cs

namespace PhoneApp1
{
    [XmlRoot("videosongs")]
    public class videosongs
    {
        [XmlElement("songsdetails")]
        public ObservableCollection<Songsdetails> Collection { get; set; }
    }

}

上面提到的是我为播放视频而编写的代码。

我在这一行中遇到错误

    var url = await YouTube.GetVideoUriAsync(song.songcode, YouTubeQuality.Quality480P);

我很多天都在尝试。如果我出错了,请帮助我。

我是初学者,无法理解为什么会出现错误。

先谢谢。

0 个答案:

没有答案