Skip to content

Commit

Permalink
Pause media playback when navigating away from mediaplay page (#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelwgn authored Aug 29, 2023
1 parent 5ab6b6f commit 90f0882
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 3 additions & 3 deletions WinUIGallery/ControlPages/MediaPlayerElementPage.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Page x:Class="AppUIBasics.ControlPages.MediaPlayerElementPage"
<Page x:Class="AppUIBasics.ControlPages.MediaPlayerElementPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:AppUIBasics"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<StackPanel>
<local:ControlExample x:Name="Example1" HeaderText="A MediaPlayerElement with transport controls.">
<local:ControlExample.Example>
<MediaPlayerElement Source="ms-appx:///Assets/SampleMedia/ladybug.wmv" MaxWidth="400" AutoPlay="False"
<MediaPlayerElement x:Name="Player1" Source="ms-appx:///Assets/SampleMedia/ladybug.wmv" MaxWidth="400" AutoPlay="False"
AreTransportControlsEnabled="True" />
</local:ControlExample.Example>
<local:ControlExample.Xaml>
Expand All @@ -20,7 +20,7 @@
</local:ControlExample>
<local:ControlExample x:Name="Example2" HeaderText="A MediaPlayerElement that autoplays the video.">
<local:ControlExample.Example>
<MediaPlayerElement Source="ms-appx:///Assets/SampleMedia/fishes.wmv" MaxWidth="400" AutoPlay="True" />
<MediaPlayerElement x:Name="Player2" Source="ms-appx:///Assets/SampleMedia/fishes.wmv" MaxWidth="400" AutoPlay="True" />
</local:ControlExample.Example>
<local:ControlExample.Xaml>
<x:String xml:space="preserve">
Expand Down
17 changes: 16 additions & 1 deletion WinUIGallery/ControlPages/MediaPlayerElementPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -21,5 +21,20 @@ public MediaPlayerElementPage()
{
this.InitializeComponent();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// Needed if this page is getting cached due to the navigation stack.
Player2.MediaPlayer.Play();
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
// Pause media playback since we are no longer visible to the user
Player1.MediaPlayer.Pause();
Player2.MediaPlayer.Pause();
}
}
}

0 comments on commit 90f0882

Please sign in to comment.