【问题标题】:WPF MediaElement source defined by converter throw NullException转换器定义的 WPF MediaElement 源抛出 NullException
【发布时间】:2021-05-02 07:00:09
【问题描述】:

我有这个转换器:

    public class MediaSourceConverter : DependencyObject, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || value.ToString().Trim() == "" || value.ToString() == "0")
        {
            // This case is running well
            return null;
        }
        else
        {
            string link = value.ToString();
            if (link.StartsWith("plugin://plugin.video.youtube/?action=play_video&videoid="))
            {
                // This case throw Null Exception in main windows code
                return ("https://www.youtube.com/watch?v=" + link.Substring(link.LastIndexOf('=') + 1));
            }
            else if (link.StartsWith("http://") || link.StartsWith("https://"))
            {
                // This case is running well
                return link;
            }
            else
            {
                // This case throw Null Exception in main windows code
                return ("https://www.youtube.com/watch?v=" + link);
            }
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML 是

<MediaElement x:Name="media_Player" 
              Source="{Binding SelectedItem.Key, ElementName=lv_Medias, Converter={StaticResource MediaSourceConverter}}"
              Width="322" Height="181" />

当转换器返回与 youtube 的链接时,会从主窗口代码中引发 Null 异常。该链接始终有效,并且可以从 Web api 中检索。

如果我在 MediaElement 绑定中删除转换器,则以 http* 开头的链接时媒体播放良好,当然,在其他情况下不播放但不会抛出错误。

有什么想法吗?

谢谢

【问题讨论】:

  • // This case throw Null Exception in main windows code return ("https://www.youtube.com/watch?v=" + link); 如果该行抛出 NRE,我会感到惊讶。 link 发生时的确切值是多少?
  • 我也很惊讶 :(,链接值是一个 youtube id,例如:“iQJNBVW8QhI”
  • 请不要说链接有效。告诉我们确切的值
  • 你能分享异常的堆栈跟踪吗?
  • 我认为问题在于 MediaElement 需要一个指向媒体文件(例如 MP4 文件)的链接,而您正在给它一个 Youtube 页面。虽然 Youtube 页面上确实播放了视频,但它本身并不是视频文件。

标签: c# wpf xaml converters mediaelement


【解决方案1】:

问题是 MediaElement 需要一个指向媒体文件(例如 MP4 文件)的链接,而您正在给它一个 Youtube 页面。虽然 Youtube 页面上确实播放了视频,但它本身并不是视频文件

【讨论】:

    【解决方案2】:

    我为那些可能对我如何处理这个问题感兴趣的人回答我自己的问题(我只需要基本的视频控制) 除了 mp4 链接,我没有其他文件,也没有 youtube 以外的其他来源

    string link = "";     // direct mp4 (or others) video link
    string source = "";   // youtube link
    if (dataSource.Key.StartsWith("plugin://plugin.video.youtube/?action=play_video&videoid="))
    {
        source = "https://www.youtube-nocookie.com/embed/" + dataSource.Key.Substring(dataSource.Key.LastIndexOf('=') + 1) + "?rel=0&amp;showinfo=0";
    }
    else if (dataSource.Key.StartsWith("http://") || dataSource.Key.StartsWith("https://"))
    {
        link = dataSource.Key;
    }
    else
    {
        source = "https://www.youtube-nocookie.com/embed/" + dataSource.Key + "?rel=0&amp;showinfo=0";
    }
    if (link == "")
    {
        wb_VideoPlayer.Source = new Uri(source);
    }
    else
    {
        wb_VideoPlayer.NavigateToString(@"<!DOCTYPE html>
            <html>
            <head>
                <meta http-equiv='Content-Type' content='text/html; charset=unicode' />
                <meta http-equiv='X-UA-Compatible' content='IE=10' /> 
                <title></title>
                <style>
                    html {
                        -ms-overflow-style: none; /* For Internet Explorer and Edge */
                    }
                    body {
                        padding: 0;
                        margin: 0;
                    }
                 </style>
            </head>
            <body>
                <div>
                     <video id='player' controls='' preload='metadata' width='421' height='237'>
                        <source src='" + link + @"' type='video/mp4' />
                    </video>
                </div>
                <script type='text/javascript'>
                    var v = document.getElementById('player');
                    var firstDisplayed = true;
    
                    // Sets the default time to 1 second to have a picture displayed
                    v.addEventListener('canplay', function() {
                        this.currentTime = 1;
                    });
                    v.addEventListener('play', function() {
                        if (firstDisplayed)
                        {
                            firstDisplayed = false;
                            v.currentTime = 0;
                        }
                        v.play();
                    });
                    v.addEventListener('ended', function() {
                        firstDisplayed = true;
                        v.pause();
                        v.currentTime = 1;
                    });
                </script>
            </body>
            </html>");
    }
    

    在 XAML 中:

    <Border HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,28,0,0"
            Width="423" Height="239" Background="Black">
         <WebBrowser x:Name="wb_VideoPlayer" 
                     ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
                     ScrollViewer.VerticalScrollBarVisibility="Hidden" />
    </Border>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 2011-03-10
      • 2011-03-13
      相关资源
      最近更新 更多