如何确定MediaElement是否正在播放?

时间:2016-08-01 08:52:54

标签: wpf

我想在我的播放器上确定视频is_playing。 但是怎么样? 请帮我。 我正在使用c#> w_pf .net framework 4.5.1 我想创建一个像这个视频的活动。

See Photo Demo

我想像那个玩家一样控制播放/暂停按钮。 如何在播放视频时控制播放/暂停按钮图像的更改。 请帮帮我

1 个答案:

答案 0 :(得分:0)

我这样做是通过使用2个按钮并将这些按钮的可见性绑定到您单击按钮时控制的bool。

基本上你必须自己跟踪播放暂停状态。媒体元素不会为你做这件事。

在主窗口xaml中添加以下内容:

private void UpdateSetting(string key, string value)
{
    var configuration = ConfigurationManager
       .OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);

    configuration.AppSettings.Settings[key].Value = value;

    configuration.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");

    // Now we need to update the setting in memory as well
    UpdateSettingInMemory(key, value);
}

private void UpdateSettingInMemory(string key, string value)
{
    var configuration = ConfigurationManager
        .OpenExeConfiguration(ConfigurationUserLevel.None);

    configuration.AppSettings.Settings[key].Value = value;

    configuration.Save(ConfigurationSaveMode.Modified);

    ConfigurationManager.RefreshSection("appSettings");
}

您还必须为此窗口命名,确保将其放入其中

// Two converters used with the visibility binding
<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    <local:InvertableBooleanToVisibilityConverter x:Key="InvertableBooleanToVisibilityConverter"/>
</Window.Resources> 

// Need to make sure we add loaded behaviour or it will crash app
<MediaElement Name="mePlayer" 
                  LoadedBehavior="Manual" 
                  Source="C:\Users\SamG\Downloads\ZW_20160102_064536A.avi"/>


<Button Content="Play" Name="Play" Click="Play_OnClick" Visibility="{Binding ElementName=mainWindow, Path=IsPaused, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<Button Content="Pause" Name="Pause" Click="Pause_OnClick" Visibility="{Binding ElementName=mainWindow, Path=IsPaused, Converter={StaticResource InvertableBooleanToVisibilityConverter}, ConverterParameter=Inverted}"/>

在c#代码中:

x:Name="mainWindow"

然后添加一个名为InvertableBooleanToVisibilityConverter的新类:

public partial class MainWindow : INotifyPropertyChanged
{
    private bool _isPaused;

    public MainWindow()
    {
        InitializeComponent();

        mePlayer.Play();
        IsPaused = true;
    }

    private void Play_OnClick(object sender, RoutedEventArgs e)
    {
        mePlayer.Play();

        IsPaused = false;
    }

    private void Pause_OnClick(object sender, RoutedEventArgs e)
    {
        mePlayer.Pause();

        IsPaused = true;
    }

    public bool IsPaused
    {
        get { return _isPaused; }
        set
        {
            _isPaused = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

复制此代码,它应该适用于您需要的内容