media element from visual tree does not display video just audio in 8.1 store app

114 Views Asked by At

I have a video to play in 8.1 windows store app and wanna to keep audio playing after navigating to other page .. i did it using visualTreeHelper so i declare a media element in app.xaml and add it to frame in app.xaml.cs , and get it in the playingPage . The problem is that the media element control play only audio and i can't see the video .. the audio keep playing after navigation , but can't see the video in the playing page (only audio ) : so this's what i put in standardstyles.xaml :

<Style x:Key="RootFrameStyle" TargetType="Frame">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Frame">

                <Grid>
<MediaElement x:Name="player" AudioCategory="BackgroundCapableMedia"   />
 <Grid>
                   <ContentPresenter />
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

and this in app.xaml.cs

rootFrame.Style = Resources["RootFrameStyle"] as Style;      

in playingPage.xaml i add MediaElement Control :

<ContentControl x:Name="videoContainer" HorizontalAlignment="Stretch" VerticalAlignment="Center" 
                            Grid.Row="0" Grid.Column="1"
                        KeyUp="VideoContainer_KeyUp"    >
                <MediaElement x:Name="player" AudioCategory="BackgroundCapableMedia"  
                    Visibility="Visible" Grid.Row="0" Grid.Column="1" AutoPlay="True" 
                    HorizontalAlignment="Center" VerticalAlignment="Center" 
                    MediaOpened="player_Opened" 
                    MediaEnded="player_Ended" 
                    MediaFailed="player_Failed" 
                              Position="10"
                    CurrentStateChanged="player_CurrentStateChanged"  />

                </ContentControl>

and in its code behind :

 DependencyObject rootGrid = VisualTreeHelper.GetChild(Window.Current.Content, 0);
     player = (MediaElement)VisualTreeHelper.GetChild(rootGrid, 0) as MediaElement;

 player.Source = video.VideoLink;
`

everything work well when i don't try to get audio working after navigation so when i don't use visual tree helper but in this situation audio work as expected but can't see the video in playing page (Only audio)

1

There are 1 best solutions below

0
On

Instead of navigating the entire page you can just navigate the frame in your maine page. This way your media element will be accessible at all pages.

 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <MediaElement Grid.Row="0"/>
        <Frame x:Name="displayedFrame" Grid.Row="1"></Frame>
    </Grid>

And whenever you have to navigate instead of using this.Frame.Navigate use displayedFrame.Navigate