본문 바로가기
프로그래밍/Windows

[WPF] YouTube Player

by Hwan,. 2022. 3. 10.
728x90
반응형

1. UI/ XML

<Window x:Class="youtube_viewer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:youtube_viewer"
        mc:Ignorable="d"
        Title="YouTube Player" Height="500" Width="800"
        Background="#FFE5E5E5"
        >

    <Grid Name="GridViewer">
        <Grid.RowDefinitions>
            <RowDefinition Height="215*"/>
            <RowDefinition Height="27*"/>
        </Grid.RowDefinitions>

        <Grid Margin="0,3,0,0" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="677*"/>
                <ColumnDefinition Width="123*"/>
            </Grid.ColumnDefinitions>

            <Button Name ="btn_play" Margin="5,10,10,10" Content="Play" Click="btn_play_Click" Grid.Column="1"/>
            <TextBox Name="txtbox_url" Margin="10,10,5,10" Grid.Column="0" TextWrapping="Wrap" Text="Input youtube url" Grid.ColumnSpan="1"/>
        </Grid>
    </Grid>
</Window>

2. C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace youtube_viewer
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        CefSharp.Wpf.ChromiumWebBrowser _browser;
        string str;

        public MainWindow()
        {
            InitializeComponent();

            _browser = new CefSharp.Wpf.ChromiumWebBrowser();
            GridViewer.Children.Add(_browser);
        }

        private void btn_play_Click(object sender, RoutedEventArgs e)
        {
            str = txtbox_url.Text.Split('=')[1].Split('&')[0];

            if (this._browser != null)
            {
                _browser.Address = $"https://www.youtube-nocookie.com/embed/{str}";
            }
        }
    }
}

3. Nuget

cef.redist.x64

728x90
반응형

댓글