播放时顶部的wpf窗口

时间:2016-08-26 06:25:49

标签: c# wpf

我想在播放时播放(音频/视频)

我想确定玩家是否正在显示。

请帮帮我。

<MediaElement x:Name="player" />

的Xaml:

<CheckBox x:Name="allwaystop" Content="Always" Checked="allwaystop_Checked" />
<CheckBox x:Name="whileplaying" Content="While Playing" Checked="whileplaying_Checked"/>
<CheckBox x:Name="nevertop" Content="Never" IsChecked="True" Checked="nevertop_Checked" />

Xaml.cs:

private void allwaystop_Checked(object sender, RoutedEventArgs e)
{
    // Always top
    Window parent = Window.GetWindow(this);
    parent.Topmost = true;           
}

private void whileplaying_Checked(object sender, RoutedEventArgs e)
{
    //WhilePlaying
    // What??
}

private void nevertop_Checked(object sender, RoutedEventArgs e)
{   
    //Never top
    Window parent = Window.GetWindow(this);
    parent.Topmost = false;
}

请帮帮我。

我将在whileplaying复选框代码中编写哪些代码?

2 个答案:

答案 0 :(得分:1)

首先,我认为单选按钮更适合此目的。

第二,你很幸运我正在挖掘你的项目...这里有更多的免费代码......虽然它可以帮助你,但还有改进的余地。以下代码实现了Player状态跟踪器,并使用跟踪器进行各种操作。您会注意到跟踪器变量已添加到您的许多方法中,因此可以相应地更新播放器状态。这可能类似于@Bolu在回答你的问题时的想法。

<RadioButton x:Name="rdobtn_AlwaysTop" Content="Always top" HorizontalAlignment="Left" Margin="10,258,0,0" VerticalAlignment="Top" GroupName="WindowZIndex" Checked="SetWindowZIndex"/>
<RadioButton x:Name="rdobtn_TopWhilePlaying" Content="Top while playing" HorizontalAlignment="Left" Margin="96,258,0,0" VerticalAlignment="Top" GroupName="WindowZIndex" Checked="SetWindowZIndex"/>
<RadioButton x:Name="rdobtn_LetUserPick" Content="Who cares" HorizontalAlignment="Left" Margin="219,258,0,0" VerticalAlignment="Top" GroupName="WindowZIndex" IsChecked="True" Checked="SetWindowZIndex" />

    public static bool IsMediaPlaying = false;//something to keep track of the player state

    private void play_Click(object sender, RoutedEventArgs e)
    {
        PlayMedia();
    }

    public void PlayMedia()
    {
        if (IsMediaPlaying == false)
        {
            //nothing is playing
            if (mePlayer.Source == new Uri(playlist[listbox4.SelectedIndex].Filepath, UriKind.RelativeOrAbsolute))
            {
                //nothing is playing, but the player already has the correct file loaded
                mePlayer.LoadedBehavior = MediaState.Play;//play it
            }
            else
            {
                //nothing is playing and the media player's loaded file(if any) is different than the selected file so play the selected file
                mePlayer.Source = new Uri(playlist[listbox4.SelectedIndex].Filepath, UriKind.RelativeOrAbsolute);
                mePlayer.LoadedBehavior = MediaState.Play;
            }
        }
        else//something is already playing
        {
            if (mePlayer.Source == new Uri(playlist[listbox4.SelectedIndex].Filepath, UriKind.RelativeOrAbsolute))
            {
                //the proper file is already playing
            }
            else
            {
                //something is playing but it looks like the user wants to play something different
                mePlayer.Source = new Uri(playlist[listbox4.SelectedIndex].Filepath, UriKind.RelativeOrAbsolute);
                mePlayer.LoadedBehavior = MediaState.Play;
            }
        }
        IsMediaPlaying = true;
        SetWindowZIndex();
    }

    private void SetWindowZIndex(object sender, RoutedEventArgs e)
    {
        SetWindowZIndex();
    }

    private void SetWindowZIndex()
    {
        if (rdobtn_AlwaysTop.IsChecked == true)
        {
            Window parent = Window.GetWindow(this);
            parent.Topmost = true;
        }
        else if (rdobtn_TopWhilePlaying.IsChecked == true)
        {
            if (IsMediaPlaying == true)
            {
                Window parent = Window.GetWindow(this);
                parent.Topmost = true;
            }
            else
            {
                Window parent = Window.GetWindow(this);
                parent.Topmost = false;
            }
        }
        else if (rdobtn_LetUserPick.IsChecked == true)
        {
            Window parent = Window.GetWindow(this);
            parent.Topmost = false;
        }
    }

    private void mePlayer_MediaFailed(object sender, ExceptionRoutedEventArgs e)
    {
        IsMediaPlaying = false;
        RoutedEventArgs newEventArgs1 = new RoutedEventArgs(Button.ClickEvent);  //Go to next
        button1.RaiseEvent(newEventArgs1);
    }

    private void mePlayer_MediaEnded(object sender, RoutedEventArgs e)
    {
        IsMediaPlaying = false;
        RoutedEventArgs newEventArgs = new RoutedEventArgs(Button.ClickEvent);  //Go to next
        button1.RaiseEvent(newEventArgs);
    }

    private void play_Click(object sender, RoutedEventArgs e)
    {
        PlayMedia();
    }

    private void pause_Click(object sender, RoutedEventArgs e)
    {
        mePlayer.LoadedBehavior = MediaState.Pause;
        IsMediaPlaying = false;
    }

    private void listbox4_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        PlayMedia();
    }

答案 1 :(得分:0)

由于MediaElement没有OnPlaying EventHandler或IsPlaying属性,因此有点困难。

你能做的是:

  1. 创建一个属性IsMediaPlaying,该属性将由您的Play PauseStop方法更新。
  2. whileplaying_Checked EventHandler中,选中IsMediaPlaying,如果为true,请设置Topmost=true;
  3. 在您的开始/播放方法中,检查whileplaying.IsChecked是否为真,设置Topmost = ture。设置IsMediaPlaying = true
  4. 同样在您的开始/播放方法中,订阅MediaElement.MediaEnded活动,也可能订阅MediaElement.MediaFailed活动。 在EventHandler设置IsMediaPlaying=false并检查是否whileplaying.IsChecked,如果设置为Topmost = false
  5. whileplaying_UnChecked EventHandler中,根据其他选项设置Topmost。 (即IsMediaPlaying然后设置Topmost=false