none
WPF 使用 MediaPlayer 打开 地址为https开头的网络视频失败 RRS feed

  • 问题

  • 使用System.Windows.Media下的 MediaPlayer类可以打开 地址为http的网络视频,但https的地址确报错

    代码:mediaplayer.Open(new Uri(@"https://www.musictime-studio.com/webfile/musical_demo/20180730045604.mp3", UriKind.Absolute));

    异常:System.NullReferenceException:“未将对象引用设置到对象的实例。” 

    请问这是什么原因

    2018年8月10日 3:21

答案

  • 你好,

    如果你不想下载下来在看的话,你可以使用web browser 控件, 然后使用SilverLight 来播放https的音视频。 或者使用第三方的工具。比如:

    https://archive.codeplex.com/?p=wpfmediakit

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 bp_l 2018年8月13日 3:23
    2018年8月13日 1:50

全部回复

  • 你好,

    这应该是一个 WPF 的已知问题。 可以先把MP3的问题写到本地,在打开。 下面的代码,你可以参考一下:

    public Window1()
            {
                InitializeComponent();
    
                MediaPlayer player = new MediaPlayer();
                var source = new Uri(@"https://www.musictime-studio.com/webfile/musical_demo/20180730045604.mp3", UriKind.Absolute);
    
                byte[] video = GetByteFromUrl(@"https://www.musictime-studio.com/webfile/musical_demo/20180730045604.mp3");
                System.IO.File.WriteAllBytes("temp1.mp3", video);
                player.Open(new Uri("temp1.mp3", UriKind.Relative));
                VideoDrawing drawing = new VideoDrawing();
                drawing.Rect = new Rect(0, 0, 300, 200);
                drawing.Player = player;
                player.Play();
                DrawingBrush brush = new DrawingBrush(drawing);
                this.Background = brush;
            }
    
    
            private byte[] GetByteFromUrl(string url)
            {
                byte[] videoData = null;
    
                using (var wc = new System.Net.WebClient())
                    videoData = wc.DownloadData(url);
                return videoData;
            }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年8月10日 6:36
  • 谢谢,不过我想实现的效果是一边缓冲一边播放,碰到稍微大点的文件,下载到本地使用的时间太长了
    2018年8月10日 8:09
  • 你好,

    如果你不想下载下来在看的话,你可以使用web browser 控件, 然后使用SilverLight 来播放https的音视频。 或者使用第三方的工具。比如:

    https://archive.codeplex.com/?p=wpfmediakit

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 bp_l 2018年8月13日 3:23
    2018年8月13日 1:50