visual studio 2015-C#ProgressBar未在媒体播放器中显示进度

本文关键字:显示 媒体播放器 studio 2015-C#ProgressBar visual | 更新日期: 2023-09-27 17:53:21

每个人。我在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>

救命!!我浪费了两天时间,在谷歌上找不到真正的解决方案。非常感谢。

visual studio 2015-C#ProgressBar未在媒体播放器中显示进度

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

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

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

有了这个:

if (videoMediaElement.CurrentState == MediaElementState.Playing)

希望这能有所帮助。