使用MyToolkit嵌入YouTube视频网络连接崩溃Windows Phone 8

时间:2013-10-11 12:26:31

标签: c# windows-phone-8 youtube mytoolkit

我已经使用MyToolkit在我的Windows phone 8应用程序中嵌入Youtube视频它可以正常使用网络连接但是当我关闭WiFi并在我的应用程序中启动视频时它会崩溃,所以我想显示一条消息告诉我用户“没有网络连接”并导航回“MainPage.xaml”而不是崩溃,这是我使用的代码

private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    var videoUri = await MyToolkit.Multimedia.YouTube.GetVideoUriAsync("Youtube_ID", MyToolkit.Multimedia.YouTubeQuality.Quality480P, MyToolkit.Multimedia.YouTubeQuality.Quality480P);
    if (videoUri != null)
        player.Source = videoUri.Uri;              
}

这是Xaml代码

<Grid HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Top" Width="Auto">
     <mmppf:MediaPlayer x:Name="player" HorizontalAlignment="Stretch" Width="Auto"/>
</Grid>

由于

2 个答案:

答案 0 :(得分:1)

试试这个  var videoUri = await MyToolkit.Multimedia.YouTube.GetVideoUriAsync(&#34; Youtube_ID&#34;,MyToolkit.Multimedia.YouTubeQuality.Quality480P)

答案 1 :(得分:0)

在try catch中围绕你的await方法:

private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    bool videoSuccess = true;
    try {
    var videoUri = await MyToolkit.Multimedia.YouTube.GetVideoUriAsync("Youtube_ID", MyToolkit.Multimedia.YouTubeQuality.Quality480P, MyToolkit.Multimedia.YouTubeQuality.Quality480P);
    if (videoUri != null)
        player.Source = videoUri.Uri; 
    }
    catch (Exception e) { // Debug and find out which exception is being thrown
    videoSuccess = false;    
    }
    if (videoSuccess == false) {
    await MessageDialog.ShowAsync("Video couldn't be played.","No Internet"); }                
    }