显示控制,如播放,在MediaElement上全屏播放视频时暂停

时间:2016-08-28 15:00:42

标签: c# wpf

我出于个人目的制作自定义媒体播放器,但在通过WPF中的MediaElement以全屏模式播放视频时遇到了问题。我无法显示播放,暂停控制按钮而不删除全屏模式。我在Google上搜索过但没有得到一个有用的话题。如果有任何解决方案,请帮助我。

我从below link获得了代码。

Xaml代码:

<Window x:Class="MediaSampleWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Media Element Sample" Height="413" Width="632">
<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="322*" />
        <RowDefinition Height="53*" />
    </Grid.RowDefinitions>
    <MediaElement x:Name="MediaEL" MediaOpened="MediaEL_MediaOpened"
                  LoadedBehavior="Manual" MouseLeftButtonUp="MediaEL_MouseLeftButtonUp"/>
    <StackPanel Orientation="Vertical" Margin="120,0" Grid.Row="1">
        <StackPanel x:Name="SPSeekBar" HorizontalAlignment="Stretch">
            <Slider x:Name="seekBar" Thumb.DragStarted="seekBar_DragStarted" 
                    Thumb.DragCompleted="seekBar_DragCompleted" />
        </StackPanel>
        <Rectangle Height="5"/>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button x:Name="btnPlay" Content="Play" Click="btnPlay_Click" 
                Width="50" Height="25"/>
            <Button x:Name="btnStop" Content="Stop" Click="btnStop_Click"  
                Width="50" Height="25"/>
            <Button x:Name="btnMoveBackward" Content="Back" Click="btnMoveBackward_Click"  
                Width="50" Height="25"/>
            <Button x:Name="btnMoveForward" Content="Forward" Click="btnMoveForward_Click"  
                Width="50" Height="25"/>
            <Button x:Name="btnOpen" Content="Open" Click="btnOpen_Click"  
                Width="50" Height="25"/>
            <Button x:Name="btnScreenShot" Content="Capture Screenshot" Click="btnScreenShot_Click" Width="120" Height="25" />
        </StackPanel>
    </StackPanel>
</Grid>

C#代码:

DispatcherTimer timer;

    #region Constructor
    public Window1()
    {
        InitializeComponent();
        IsPlaying(false);
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(200);
        timer.Tick += new EventHandler(timer_Tick);
    }
    #endregion

    #region IsPlaying(bool)
    private void IsPlaying(bool bValue)
    {
        btnStop.IsEnabled = bValue;
        btnMoveBackward.IsEnabled = bValue;
        btnMoveForward.IsEnabled = bValue;
        btnPlay.IsEnabled = bValue;
        btnScreenShot.IsEnabled = bValue;
        seekBar.IsEnabled = bValue;
    } 
    #endregion

    #region Play and Pause
    private void btnPlay_Click(object sender, RoutedEventArgs e)
    {
        IsPlaying(true);
        if (btnPlay.Content.ToString() == "Play")
        {
            MediaEL.Play();
            btnPlay.Content = "Pause";
        }
        else
        {
            MediaEL.Pause();
            btnPlay.Content = "Play";
        }
    } 
    #endregion

    #region Stop
    private void btnStop_Click(object sender, RoutedEventArgs e)
    {
        MediaEL.Stop();
        btnPlay.Content = "Play";
        IsPlaying(false);
        btnPlay.IsEnabled = true;
    } 
    #endregion

    #region Back and Forward
    private void btnMoveForward_Click(object sender, RoutedEventArgs e)
    {
        MediaEL.Position = MediaEL.Position + TimeSpan.FromSeconds(10);
    }

    private void btnMoveBackward_Click(object sender, RoutedEventArgs e)
    {
        MediaEL.Position = MediaEL.Position - TimeSpan.FromSeconds(10);
    } 
    #endregion

    #region Open Media
    private void btnOpen_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
        ofd.Filter = "Video Files (*.wmv)|*.wmv";
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            MediaEL.Source = new Uri(ofd.FileName);
            btnPlay.IsEnabled = true;
        }
    }
    #endregion

    #region Capture Screenshot
    private void btnScreenShot_Click(object sender, RoutedEventArgs e)
    {
        byte[] screenshot = MediaEL.GetScreenShot(1, 90);
        FileStream fileStream = new FileStream(@"Capture.jpg", FileMode.Create, FileAccess.ReadWrite);
        BinaryWriter binaryWriter = new BinaryWriter(fileStream);
        binaryWriter.Write(screenshot);
        binaryWriter.Close();
    } 
    #endregion

    #region Seek Bar
    private void MediaEL_MediaOpened(object sender, RoutedEventArgs e)
    {
        if (MediaEL.NaturalDuration.HasTimeSpan)
        {
            TimeSpan ts = MediaEL.NaturalDuration.TimeSpan;
            seekBar.Maximum = ts.TotalSeconds;
            seekBar.SmallChange = 1;
            seekBar.LargeChange = Math.Min(10, ts.Seconds / 10);
        }
        timer.Start();
    }

    bool isDragging = false;

    void timer_Tick(object sender, EventArgs e)
    {
        if (!isDragging)
        {
            seekBar.Value = MediaEL.Position.TotalSeconds;
            currentposition = seekBar.Value;
        }
    } 

    private void seekBar_DragStarted(object sender, DragStartedEventArgs e)
    {
        isDragging = true;
    }

    private void seekBar_DragCompleted(object sender, DragCompletedEventArgs e)
    {
        isDragging = false;
        MediaEL.Position = TimeSpan.FromSeconds(seekBar.Value);
    }
    #endregion

    #region FullScreen
    [DllImport("user32.dll")]
    static extern uint GetDoubleClickTime();

    System.Timers.Timer timeClick = new System.Timers.Timer((int)GetDoubleClickTime())
    {
        AutoReset = false
    };

    bool fullScreen = false;
    double currentposition = 0;

    private void MediaEL_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (!timeClick.Enabled)
        {
            timeClick.Enabled = true;
            return;
        }

        if (timeClick.Enabled)
        {
            if (!fullScreen)
            {
                LayoutRoot.Children.Remove(MediaEL);
                this.Background = new SolidColorBrush(Colors.Black);
                this.Content = MediaEL;
                this.WindowStyle = WindowStyle.None;
                this.WindowState = WindowState.Maximized;
                MediaEL.Position = TimeSpan.FromSeconds(currentposition);
            }
            else
            {
                this.Content = LayoutRoot;
                LayoutRoot.Children.Add(MediaEL);
                this.Background = new SolidColorBrush(Colors.White);
                this.WindowStyle = WindowStyle.SingleBorderWindow;
                this.WindowState = WindowState.Normal;
                MediaEL.Position = TimeSpan.FromSeconds(currentposition);
            }
            fullScreen = !fullScreen;
        }
    }
    #endregion

0 个答案:

没有答案