C#ProgressBar未在媒体播放器

时间:2016-06-30 13:47:15

标签: c# visual-studio-2015 uwp-xaml

大家。我在使用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>

PLZ帮助!!我浪费了2天,我发现谷歌没有真正的解决方案。谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

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

  3. 有了这个:

    if (videoMediaElement.CurrentState == MediaElementState.Playing)
    

    希望这有帮助。