0

我已经使用 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>

谢谢

4

2 回答 2

1

试试这个 var videoUri = await MyToolkit.Multimedia.YouTube.GetVideoUriAsync("Youtube_ID", MyToolkit.Multimedia.YouTubeQuality.Quality480P)

于 2015-02-03T12:28:35.523 回答
0

将您的 await 方法包围在 try catch 中:

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"); }                
    }
于 2014-07-31T00:46:41.743 回答