Karen Tsirunyan
Karen Tsirunyan

Reputation: 1998

Embedding Youtube videos in WPF application

I am trying to play a Youtube video right from my simple WPF application.

I have investigated several ways to do that:

  1. Using WebBrowser control In this case seems you just need to apply the Youtube video URL to WebBrowser source field and just make a little changes in the URL itself, like:

    Original URL = https://www.youtube.com/watch?v=xxxxxxx
    Changed URL = https://www.youtube.com/v/xxxxxxx

  2. The other approach just suggests to use ShockWave Flash object, which basically should have pretty the same result (video tutorial below)

https://www.youtube.com/watch?v=714w3HQDTcA

I have tried both cases and seems that the solutions are old, as I get the following error: enter image description here

So the question is:

Is there any way to simply play a youtube video in WPF app, with or without the described soultions. Or maybe I am doing something wrong. Thank you.

Upvotes: 2

Views: 5561

Answers (4)

Maxence
Maxence

Reputation: 13296

Use the Nuget: Microsoft.Web.WebView2

<wpf:WebView2 Source="https://www.youtube.com/embed/<Video ID>"/>

Upvotes: 0

        string html = "<html><head>";
        html += "<meta content='IE=Edge' http-equiv='X-UA-Compatible'/>";

        html += "<iframe id='video' src= 'https://www.youtube.com/embed/{0}' width='640' height='360' frameborder='0' allow = \"autoplay; encrypted-media\" allowFullScreen></iframe>";

        html += "</body></html>";
        this.webBrowser1.DocumentText = string.Format(html, txtboxLink.Text.Split('=')[1]);

This will work for me. Try this for your play button event.

Upvotes: 2

Moustafa Khalil
Moustafa Khalil

Reputation: 164

I usually do it this way:

https://www.youtube.com/embed/XXXXXX

XXXXX is your Video Code

check also this answer how do I embed a youtube video into a WPF MediaElement and save the video?

Upvotes: 1

user10642976
user10642976

Reputation:

The cause of your problem

When you're using the native WebBrowser control, remember that it's powered by Internet Explorer (not sure if Windows 10 uses Edge to handle that control), both outdated browsers that likely don't support the new YouTube player. YouTube switched its video player in favor of the new HTML5 one and flash stopped being supported thus, the ShockWave Flash Object not working.

A possible solution

What you can try to do is using a more modern embedded browser. In C#, your best option might be a CEF port to C#, CefSharp. CEF stands for Chromium Embedded Framework and, because Chromium is the base for Google Chrome, just keep the library updated and you'll always have support for the newest features in Web browsers.

Upvotes: 2

Related Questions