hi there;
I’ll show you how to play a youtube video in C# Form Application without using Shockwave Flash Player. Youtube had support flash play previous but now It does not support it. So for playing the video I used webBrowser component.
Let’s start creating project. Click File / New /Project in Visual Studio. Then Create Windows Form Application.
I created the project with Label, Button, TextBox and WebBrowser Component.
Here is the project output:
Click double to “Button Component” and add the following code into “Form1 class” as public
1 2 3 4 5 6 7 8 9 10 11 | string _url; public string VideoID { get { var yMatch = new Regex(@"http(?:s?)://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)([\w\-]+)(&(amp;)?[\w\?=]*)?").Match(_url); return yMatch.Success ? yMatch.Groups[1].Value : String.Empty; } } |
Then add the code into button1_Click method
1 2 3 4 5 6 7 8 9 10 11 12 | private void button1_Click(object sender, EventArgs e) { _url = textBox1.Text; webBrowser1.DocumentText = String.Format( "<html><head>" + "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"/>" + "</head><body>" + "<iframe width=\"100%\" height=\"315\" src=\"https://www.youtube.com/embed/{0}?autoplay=1\"" + "frameborder = \"0\" allow = \"autoplay; encrypted-media\" allowfullscreen></iframe>" + "</body></html>", VideoID); } |
Output:
All Codes: Play YouTube Video in C# Form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public partial class Form1 : Form { string _url; public string VideoID { get { var yMatch = new Regex(@"http(?:s?)://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)([\w\-]+)(&(amp;)?[\w\?=]*)?").Match(_url); return yMatch.Success ? yMatch.Groups[1].Value : String.Empty; } } public Form1() { InitializeComponent(); textBox1.Text = "https://www.youtube.com/watch?v=MnXSeqKQuqo"; } private void button1_Click(object sender, EventArgs e) { _url = textBox1.Text; webBrowser1.DocumentText = String.Format( "<html><head>" + "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"/>" + "</head><body>" + "<iframe width=\"100%\" height=\"315\" src=\"https://www.youtube.com/embed/{0}?autoplay=1\"" + "frameborder = \"0\" allow = \"autoplay; encrypted-media\" allowfullscreen></iframe>" + "</body></html>", VideoID); } } |
Очень, и очень давно искал это!