9 November 2025 Sunday 10:49:16


C# ile YouTube Video Oynatma

C# ile YouTube Video Oynatma

Merhaba arkadaşlar bu yazıda sizlere C# Form Ekranında,  youtube videolarını nasıl oynatacağınız göstereceğiz. Sözü fazla uzatmadan Form Componentlerinin listesini aşağıdaki gibi veriyorum. Bir form uygulaması açıp tasarımı oluşturuyoruz.

Kullandığım Componentler, Label, TextBox, Button ve WebBrowser

C# Youtube Video Player Form Tasarımı

Oynatıcı tasarımını tamamladıktan sonra Aç butonuna çift tıklayıp aşağıdaki kodları yazıyoruz.

Button Click Metodundan önce Youtube videosunun ID değerini almak için aşağıdaki REGEX kodunu Form1 classı içinde oluşturuyoruz. Bu kod bize videoyu embed yapabilmek için gerekli olacak.

Form1 classı içinde oluşturuyoruz.

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;
            }
        }

Button Click Olayı:

private void simpleButton1_Click(object sender, EventArgs e)
        {
            URL = txtBrowser.Text;
            webBrowser1.DocumentText = String.Format("", VideoID);
        }

KODLARIN TAMAMI Aşağıdaki gibidir.

using DevExpress.XtraEditors; 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DenemeYedekleme
{
    public partial class YouTubeVideo : DevExpress.XtraEditors.XtraForm
    {
        public YouTubeVideo()
        {
            InitializeComponent();
        }


        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;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            URL = txtBrowser.Text;
            webBrowser1.DocumentText = String.Format("", VideoID);
        }


    }
}

 

img

ibrahim ÖZKAN