C# ProgressBar 在媒体播放器中不显示进度

C# ProgressBar not showing progress in media player

各位。我在用 C# 开发测试媒体播放器时遇到了问题。我想我放了所有代码,但是当我打开文件时,它开始播放但进度条没有移动。这是代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Media;
using Windows.Storage;
using Windows.Storage.Pickers;
using System.Threading;
using System.Windows;


namespace App1

{


public sealed partial class MainPage : Page
{


    public MainPage()
    {
        this.InitializeComponent();

        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick +=  timer_Tick;
        timer.Start();

    }
    TimeSpan _position;

    private async void Open_Click(object sender, RoutedEventArgs e)
    {
        var openPicker = new FileOpenPicker();

        openPicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;

        openPicker.FileTypeFilter.Add(".wmv");

        openPicker.FileTypeFilter.Add(".mp4");

        openPicker.FileTypeFilter.Add(".mp3");
        var file = await openPicker.PickSingleFileAsync();

        if (file != null)

        {

            var stream = await file.OpenAsync(FileAccessMode.Read);


            videoMediaElement.SetSource(stream, file.ContentType);


        }
    }

    private void Play_Click(object sender, RoutedEventArgs e)
    {
        if(videoMediaElement.PlaybackRate != 1 )
        {
            videoMediaElement.DefaultPlaybackRate = 1;

        }

        videoMediaElement.Play();

    }

    private void Stop_Click(object sender, RoutedEventArgs e)
    {
        videoMediaElement.Stop();
    }

    private void ProgressBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {

    }

    private void timer_Tick(object sender, object e)
    {
        if (videoMediaElement.Source != null && videoMediaElement.NaturalDuration.HasTimeSpan)
        {
           ProgressBar.Minimum =  0;
           ProgressBar.Maximum = videoMediaElement.NaturalDuration.TimeSpan.TotalSeconds;
           ProgressBar.Value = videoMediaElement.Position.TotalSeconds;
        }
    }

    private void videoMediaElement_MediaOpened(object sender, RoutedEventArgs e)
    {
        _position = videoMediaElement.NaturalDuration.TimeSpan;
        ProgressBar.Minimum = 0;
        ProgressBar.Maximum = _position.TotalSeconds;
    }
}
}

和XAML:

<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button x:Name="Open" Content="Open" HorizontalAlignment="Left" Margin="60,564,0,0" VerticalAlignment="Top" Width="299" Click="Open_Click"/>
    <Button x:Name="Play" Content="Play" HorizontalAlignment="Left" Margin="393,564,0,0" VerticalAlignment="Top" Width="299" Click="Play_Click"/>
    <Button x:Name="Stop" Content="Stop" HorizontalAlignment="Left" Margin="737,564,0,0" VerticalAlignment="Top" Width="299" Click="Stop_Click"/>
    <ProgressBar x:Name="ProgressBar" 

                 HorizontalAlignment="Left" Height="16" Margin="60,665,0,0" VerticalAlignment="Top" Width="1185" ValueChanged="ProgressBar_ValueChanged"/>
    <Slider x:Name="slider" HorizontalAlignment="Left" Margin="1082,557,0,0" VerticalAlignment="Top" Width="163"/>
    <MediaElement x:Name="videoMediaElement" HorizontalAlignment="Left" Height="491.884" Margin="4.22,58.023,0,0" VerticalAlignment="Top" Width="1270.501" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto" MediaOpened="videoMediaElement_MediaOpened">
        <MediaElement.RenderTransform>
            <CompositeTransform Rotation="0.111"/>
        </MediaElement.RenderTransform>
    </MediaElement>

</Grid>

请帮助!!我浪费了 2 天,在 google 上没有找到真正的解决方案。谢谢!

嗯,我查看了您的代码并发现了您的问题。似乎 SetSource() 方法并没有真正设置 mediaelement 的来源。我不知道为什么。也许这里有人会帮助你。我可以指出几件事。

  1. 你在电影开始前开始填充这个进度条。这是一个问题。您可以不在 MainPage 构造函数中而是在您的 Play_Click 事件中初始化您的计时器,或者在这种情况下您的电影在您的 Open_Click 事件中自动启动。
  2. 正如我所提到的,因为源总是空的,所以进度条永远不会填满。幸运的是,有一种方法可以检查视频元素当前是否为 运行。替换为:

    if (videoMediaElement.Source != null && videoMediaElement.NaturalDuration.HasTimeSpan)

有了这个:

if (videoMediaElement.CurrentState == MediaElementState.Playing)

希望对您有所帮助。