转换器定义的 WPF MediaElement 源抛出 NullException

问题描述

我有这个转换器:

    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,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* 开头时媒体播放良好,当然,在其他情况下不会播放但不会引发错误

有什么想法吗?

谢谢

解决方法

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

,

我为那些可能对我如何处理这个问题感兴趣的人回答我自己的问题(我只需要基本的视频控制) 我除了 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"
        Width="423" Height="239" Background="Black">
     <WebBrowser x:Name="wb_VideoPlayer" 
                 ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
                 ScrollViewer.VerticalScrollBarVisibility="Hidden" />
</Border>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...