Reputation: 85
I want to topmost my window while its playing(audio/video)
I want to determine player is actually displaying.
Please help me.
<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;
}
Please help me.
Which code I will write inside the whileplaying checkbox code?
Upvotes: 0
Views: 186
Reputation: 2317
First, I think radio buttons are better suited for that purpose.
Second, you lucky I'm digging your project... here's more free code... There is room for improvement, though it will get you going. The following code implements a Player state tracker, and uses the tracker for various operations. You'll notice the tracker variable has been added to many of your methods so the Player state can be updated accordingly. This is probably similar to what @Bolu had in mind when he answered your question.
<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();
}
Upvotes: 1
Reputation: 8786
It is a bit difficult, as there is no OnPlaying EventHandler or IsPlaying Property for MediaElement
.
What you could do is:
IsMediaPlaying
which will be updated by your Play
Pause
and Stop
method. whileplaying_Checked
EventHandler, check IsMediaPlaying
and if it is true set Topmost=true
;whileplaying.IsChecked
, if it is true set the Topmost = ture
. Set IsMediaPlaying = true
MediaElement.MediaEnded
Event and maybe also the MediaElement.MediaFailed
Event.
And in the EventHandler
set IsMediaPlaying=false
and check if whileplaying.IsChecked
, when it is true set Topmost = false
.whileplaying_UnChecked
EventHandler, set Topmost
according to other options. (i.e if IsMediaPlaying
then set Topmost=false
)Upvotes: 0