첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestSolution.zip
다운로드
TestSolution.z01
다운로드

[TestProject 프로젝트]

▶ MainWindow.xaml

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

 

728x90

 

▶ MainWindow.xaml.cs

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
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요