첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ 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"
    Title="MediaElement 클래스 : 동영상 재생하기"
    Width="800"
    Height="600"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"    />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto"   />
        </Grid.RowDefinitions>
        <MediaElement x:Name="mediaElement" Grid.Row="0" Grid.Column="0"
            Margin="5"
            ScrubbingEnabled="True"
            LoadedBehavior="Manual"
            Source="Sample/minions.mp4" />
        <ScrollBar Name="positionScrollBar" Grid.Row="1"
            Orientation="Horizontal"
            VerticalAlignment="Center"
            Visibility="Hidden" />
        <StackPanel Grid.Row="2"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Margin="5"
            Orientation="Horizontal">
            <StackPanel.Resources>
                <Style TargetType="Button">
                    <Setter Property="Margin" Value="2" />
                </Style>
            </StackPanel.Resources>
            <Button x:Name="playButton">
                <Image Source="Image/play.png" />
            </Button>
            <Button x:Name="pauseButton"
                IsEnabled="False"
                Opacity="0.5">
                <Image Source="Image/pause.png" />
            </Button>
            <Button x:Name="stopButton">
                <Image Source="Image/stop.png" />
            </Button>
            <Button x:Name="restartButton">
                <Image Source="Image/restart.png"/>
            </Button>
            <Button Name="previousButton">
                <Image Source="Image/previous.png" />
            </Button>
            <Button x:Name="nextButton">
                <Image Source="Image/next.png" />
            </Button>
            <Label Width="5" />
            <Button Name="slowerButton">
                <Image Source="Image/slower.png" />
            </Button>
            <Button x:Name="fasterButton">
                <Image Source="Image/faster.png" />
            </Button>
            <Label
                Margin="5 0 0 0"
                VerticalAlignment="Center"
                Content="위치 :" />
            <TextBox Name="positionTextBox"
                Width="35"
                Height="25"
                HorizontalContentAlignment="Right"
                VerticalContentAlignment="Center"
                Text="0.0" />
            <Button Name="setPositionButton"
                Margin="5 0 0 0"
                Padding="5"
                Height="30"
                Content="위치 설정" />
        </StackPanel>
    </Grid>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Threading;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 디스패처 타이머
        /// </summary>
        private DispatcherTimer dispatcherTimer;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            Loaded                        += Window_Loaded;
            this.mediaElement.MediaOpened += mediaElement_MediaOpened;
            this.playButton.Click         += playButton_Click;
            this.pauseButton.Click        += pauseButton_Click;
            this.stopButton.Click         += stopButton_Click;
            this.restartButton.Click      += restartButton_Click;
            this.previousButton.Click     += previousButton_Click;
            this.nextButton.Click         += nextButton_Click;
            this.fasterButton.Click       += fasterButton_Click;
            this.slowerButton.Click       += slowerButton_Click;
            this.setPositionButton.Click  += setPositionButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.dispatcherTimer = new DispatcherTimer();

            this.dispatcherTimer.Interval = TimeSpan.FromSeconds(0.1d);

            this.dispatcherTimer.Tick += dispatcherTimer_Tick;

            this.mediaElement.Stop();
        }

        #endregion
        #region 디스패처 타이머 틱 처리하기 - dispatcherTimer_Tick(sender, e)

        /// <summary>
        /// 디스패처 타이머 틱 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            ShowPosition();
        }

        #endregion
        #region 미디어 엘리먼트 미디어 오픈시 처리하기 - mediaElement_MediaOpened(sender, e)

        /// <summary>
        /// 미디어 엘리먼트 미디어 오픈시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            this.positionScrollBar.Minimum    = 0;
            this.positionScrollBar.Maximum    = this.mediaElement.NaturalDuration.TimeSpan.TotalSeconds;
            this.positionScrollBar.Visibility = Visibility.Visible;
        }

        #endregion
        #region Play 버튼 클릭시 처리하기 - playButton_Click(sender, e)

        /// <summary>
        /// Play 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void playButton_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Play();

            SetButtnsEnabled(true);
        }

        #endregion
        #region Pause 버튼 클릭시 처리하기 - pauseButton_Click(sender, e)

        /// <summary>
        /// Pause 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pauseButton_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Pause();

            SetButtnsEnabled(false);
        }

        #endregion
        #region Stop 버튼 클릭시 처리하기 - stopButton_Click(sender, e)

        /// <summary>
        /// Stop 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void stopButton_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Stop();

            SetButtnsEnabled(false);

            ShowPosition();
        }

        #endregion
        #region Restart 버튼 클릭시 처리하기 - restartButton_Click(sender, e)

        /// <summary>
        /// Restart 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void restartButton_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Stop();

            this.mediaElement.Play();

            SetButtnsEnabled(true);
        }

        #endregion
        #region Previous 버튼 클릭시 처리하기 - previousButton_Click(sender, e)

        /// <summary>
        /// Previous 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void previousButton_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Position -= TimeSpan.FromSeconds(10);

            ShowPosition();
        }

        #endregion
        #region Next 버튼 클릭시 처리하기 - nextButton_Click(sender, e)

        /// <summary>
        /// Next 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void nextButton_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Position += TimeSpan.FromSeconds(10);

            ShowPosition();
        }

        #endregion
        #region Faster 버튼 클릭시 처리하기 - fasterButton_Click(sender, e)

        /// <summary>
        /// Faster 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void fasterButton_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.SpeedRatio *= 1.5d;
        }

        #endregion
        #region Slower 버튼 클릭시 처리하기 - slowerButton_Click(sender, e)

        /// <summary>
        /// Slower 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void slowerButton_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.SpeedRatio /= 1.5d;
        }

        #endregion
        #region 위치 설정 버튼 클릭시 처리하기 - setPositionButton_Click(sender, e)

        /// <summary>
        /// 위치 설정 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void setPositionButton_Click(object sender, RoutedEventArgs e)
        {
            TimeSpan timespan = TimeSpan.FromSeconds(double.Parse(this.positionTextBox.Text));

            this.mediaElement.Position = timespan;

            ShowPosition();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 위치 보여주기 - ShowPosition()

        /// <summary>
        /// 위치 보여주기
        /// </summary>
        private void ShowPosition()
        {
            this.positionScrollBar.Value = this.mediaElement.Position.TotalSeconds;

            this.positionTextBox.Text = this.mediaElement.Position.TotalSeconds.ToString("0.0");
        }

        #endregion
        #region 버튼들 이용 가능 여부 설정하기 - SetButtnsEnabled(isPlaying)

        /// <summary>
        /// 버튼들 이용 가능 여부 설정하기
        /// </summary>
        /// <param name="isPlaying">재생 여부</param>
        private void SetButtnsEnabled(bool isPlaying)
        {
            if(isPlaying)
            {
                this.playButton.IsEnabled  = false;
                this.pauseButton.IsEnabled = true;
                this.playButton.Opacity    = 0.5d;
                this.pauseButton.Opacity   = 1.0d;
            }
            else
            {
                this.playButton.IsEnabled  = true;
                this.pauseButton.IsEnabled = false;
                this.playButton.Opacity    = 1.0d;
                this.pauseButton.Opacity   = 0.5d;
            }

            this.dispatcherTimer.IsEnabled = isPlaying;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요