[C#/WPF] 실시간 스트리밍 프토로콜(RTSP)을 사용해 동영상 재생하기
■ 실시간 스트리밍 프토로콜(RTSP)을 사용해 동영상을 재생하는 방법을 보여준다. [TestProject 프로젝트] ▶ MainWindow.xaml
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:control="clr-namespace:TestLibrary;assembly=TestLibrary" Width="800" Height="600" Title="실시간 스트리밍 프토로콜(RTSP)을 사용해 동영상 재생하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Border Grid.Row="0" BorderThickness="1" BorderBrush="Black"> <control:StreamPlayerControl x:Name="streamPlayerControl" /> </Border> <Grid Grid.Row="2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBox x:Name="urlTextBox" BorderBrush="Black" VerticalContentAlignment="Center" Text="rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov" /> <TextBox x:Name="statusLabel" Grid.Column="1" Margin="10 0 0 0" Width="100" BorderBrush="Black" IsEnabled="False" VerticalContentAlignment="Center" /> <Button x:Name="playButton" Grid.Column="2" Margin="10 0 0 0 " Width="100" Height="30" Content="재생하기" /> <Button x:Name="stopButton" Grid.Column="3" Margin="10 0 0 0 " Width="100" Height="30" IsEnabled="False" Content="중단하기" /> <Button x:Name="imageButton" Grid.Column="4" Margin="10 0 0 0 " Width="100" Height="30" IsEnabled="False" Content="이미지" /> </Grid> </Grid> </Window> |
▶ MainWindow.xaml.cs
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
using Microsoft.Win32; using System; using System.Windows; using TestLibrary; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.streamPlayerControl.StreamFailed += streamPlayerControl_StreamHandler; this.streamPlayerControl.StreamStarted += streamPlayerControl_StreamHandler; this.streamPlayerControl.StreamStopped += streamPlayerControl_StreamHandler; this.playButton.Click += playButton_Click; this.stopButton.Click += stopButton_Click; this.imageButton.Click += imageButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 스트림 재생기 컨트롤 스트림 핸들러 - streamPlayerControl_StreamHandler(sender, e) /// <summary> /// 스트림 재생기 컨트롤 스트림 핸들러 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void streamPlayerControl_StreamHandler(object sender, RoutedEventArgs e) { SetButtonEnabled(); if(e.RoutedEvent.Name == "StreamStarted") { this.statusLabel.Text = "재생"; } else if(e.RoutedEvent.Name == "StreamFailed") { statusLabel.Text = "실패"; MessageBox.Show ( ((StreamFailedEventArgs)e).Error, "에러", MessageBoxButton.OK, MessageBoxImage.Error ); } else if(e.RoutedEvent.Name == "StreamStopped") { statusLabel.Text = "중단"; } } #endregion #region 재생하기 버튼 클릭시 처리하기 - playButton_Click(sender, e) /// <summary> /// 재생하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void playButton_Click(object sender, RoutedEventArgs e) { Uri uri = new Uri(this.urlTextBox.Text); this.streamPlayerControl.StartPlay(uri); this.statusLabel.Text = "연결중..."; } #endregion #region 중단하기 버튼 클릭시 처리하기 - stopButton_Click(sender, e) /// <summary> /// 중단하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stopButton_Click(object sender, RoutedEventArgs e) { this.streamPlayerControl.Stop(); } #endregion #region 이미지 버튼 클릭시 처리하기 - stopButton_Click(sender, e) /// <summary> /// 이미지 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void imageButton_Click(object sender, RoutedEventArgs e) { SaveFileDialog dialog = new SaveFileDialog { Filter = "비트맵 이미지|*.bmp" }; if(dialog.ShowDialog() == true) { this.streamPlayerControl.GetCurrentFrame().Save(dialog.FileName); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 버튼 이용 가능 여부 설정하기 - SetButtonEnabled() /// <summary> /// 버튼 이용 가능 여부 설정하기 /// </summary> private void SetButtonEnabled() { this.playButton.IsEnabled = !this.streamPlayerControl.IsPlaying; this.stopButton.IsEnabled = this.streamPlayerControl.IsPlaying; this.imageButton.IsEnabled = this.streamPlayerControl.IsPlaying; } #endregion } } |
TestSolution.zip