Archived
1
0

Added ad to player

This commit is contained in:
Michael Gordeev
2019-04-13 16:23:54 +03:00
parent d28442ba1d
commit 853a219456
8 changed files with 221 additions and 10 deletions
@@ -0,0 +1,98 @@
<UserControl
x:Class="FoxTube.Controls.Adverts.PlayerAdvert"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="53"
d:DesignWidth="1920"
Name="card"
Visibility="Collapsed"
Height="0"
Opacity="0">
<UserControl.Resources>
<Storyboard x:Name="show">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="card">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:0.1">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetName="card" Storyboard.TargetProperty="Height">
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="53" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetName="card" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetName="countdown" Storyboard.TargetProperty="Value" EnableDependentAnimation="True" From="0" To="100" Duration="0:0:10"/>
</Storyboard>
<Storyboard x:Name="hide">
<DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetName="card" Storyboard.TargetProperty="Height">
<EasingDoubleKeyFrame KeyTime="0:0:0" Value="53" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="card" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0:0:0" Value="1" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0" />
</DoubleAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="card">
<DiscreteObjectKeyFrame KeyTime="0:0:0.3">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:0.4">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<Grid Height="53" Background="{ThemeResource SystemChromeMediumColor}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Name="grid" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1" Padding="5">
<StackPanel Orientation="Horizontal">
<TextBlock Name="title" FontWeight="Bold" Text="Title"/>
<TextBlock Name="info" Foreground="Gray" VerticalAlignment="Center" Margin="10,0" Style="{StaticResource CaptionTextBlockStyle}" Text=""/>
</StackPanel>
<TextBlock Name="description" Text="Description" TextTrimming="CharacterEllipsis"/>
</StackPanel>
<Button Name="cta" Content="CALL TO ACTION" Grid.Column="2" Margin="10,0"/>
</Grid>
<Button Grid.Column="1" Width="50" Height="50" HorizontalAlignment="Right" FontFamily="Segoe MDL2 Assets" Content="&#xE106;" Background="Transparent" Click="Button_Click"/>
<ProgressBar Name="countdown" VerticalAlignment="Bottom" Grid.Row="1" Grid.ColumnSpan="2"/>
</Grid>
</UserControl>
@@ -0,0 +1,82 @@
using Microsoft.Advertising.WinRT.UI;
using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace FoxTube.Controls.Adverts
{
public sealed partial class PlayerAdvert : UserControl
{
NativeAdsManagerV2 manager = new NativeAdsManagerV2(SecretsVault.AppId, SecretsVault.AdUnitId);
NativeAdV2 advert;
DispatcherTimer timer = new DispatcherTimer()
{
Interval = TimeSpan.FromMilliseconds(10000)
};
public PlayerAdvert()
{
InitializeComponent();
manager.AdReady += AdReady;
manager.ErrorOccurred += ErrorOccurred;
timer.Tick += (s, e) =>
{
timer.Stop();
hide.Begin();
};
}
private void ErrorOccurred(object sender, NativeAdErrorEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Error has occured while loading ad");
}
private void AdReady(object sender, NativeAdReadyEventArgs arg)
{
advert = arg.NativeAd;
title.Text = advert.Title;
description.Text = Methods.GuardFromNull(advert.Description);
if (advert.AdIcon != null)
{
grid.Children.Insert(0, advert.AdIcon);
advert.AdIcon.HorizontalAlignment = HorizontalAlignment.Left;
advert.AdIcon.Margin = new Thickness(4);
advert.AdIcon.Width = double.NaN;
advert.AdIcon.Height = double.NaN;
advert.AdIcon.SizeChanged += (s, e) => grid.ColumnDefinitions[0].Width = new GridLength(advert.AdIcon.ActualWidth + 8);
}
cta.Content = Methods.GuardFromNull(advert.CallToActionText);
cta.Visibility = string.IsNullOrWhiteSpace(advert.CallToActionText) ? Visibility.Collapsed : Visibility.Visible;
info.Text += advert.Price;
info.Text += " " + advert.Rating;
if (cta.Visibility == Visibility.Collapsed)
advert.RegisterAdContainer(grid);
else
advert.RegisterAdContainer(grid, new List<FrameworkElement> { cta });
timer.Start();
show.Begin();
}
public void PushAdvert()
{
manager.RequestAd();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
timer.Stop();
hide.Begin();
}
}
}
+10
View File
@@ -1,4 +1,5 @@
using FoxTube.Controls; using FoxTube.Controls;
using FoxTube.Controls.Adverts;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@@ -28,6 +29,7 @@ namespace FoxTube
public event QualityChangedEventHandler QualityChanged; public event QualityChangedEventHandler QualityChanged;
public MediaElement Player; public MediaElement Player;
public PlayerAdvert Advert;
public IReadOnlyList<ClosedCaptionTrackInfo> ClosedCaptions { get; set; } public IReadOnlyList<ClosedCaptionTrackInfo> ClosedCaptions { get; set; }
public MediaStreamInfoSet MediaStreams { get; set; } public MediaStreamInfoSet MediaStreams { get; set; }
@@ -44,6 +46,8 @@ namespace FoxTube
{ {
isReady = true; isReady = true;
Advert = GetTemplateChild("ad") as PlayerAdvert;
(GetTemplateChild("close") as Button).Click += Close_Click; (GetTemplateChild("close") as Button).Click += Close_Click;
(GetTemplateChild("compactClose") as Button).Click += Close_Click; (GetTemplateChild("compactClose") as Button).Click += Close_Click;
@@ -200,6 +204,8 @@ namespace FoxTube
(GetTemplateChild("dragholder") as Button).Visibility = Visibility.Visible; (GetTemplateChild("dragholder") as Button).Visibility = Visibility.Visible;
(GetTemplateChild("captions") as LiveCaptions).Size = 15; (GetTemplateChild("captions") as LiveCaptions).Size = 15;
(GetTemplateChild("root") as Grid).RowDefinitions[1].Height = new GridLength(0);
} }
public void SetMinimized() public void SetMinimized()
@@ -225,6 +231,8 @@ namespace FoxTube
(GetTemplateChild("maximize") as Button).Visibility = Visibility.Visible; (GetTemplateChild("maximize") as Button).Visibility = Visibility.Visible;
(GetTemplateChild("captions") as LiveCaptions).Size = 15; (GetTemplateChild("captions") as LiveCaptions).Size = 15;
(GetTemplateChild("root") as Grid).RowDefinitions[1].Height = new GridLength(0);
} }
public void SetNormal() public void SetNormal()
@@ -249,6 +257,8 @@ namespace FoxTube
(GetTemplateChild("captions") as LiveCaptions).Size = 24; (GetTemplateChild("captions") as LiveCaptions).Size = 24;
(GetTemplateChild("dragholder") as Button).Visibility = Visibility.Collapsed; (GetTemplateChild("dragholder") as Button).Visibility = Visibility.Collapsed;
(GetTemplateChild("root") as Grid).RowDefinitions[1].Height = new GridLength(1, GridUnitType.Auto);
} }
public void SetMeta(string title, string channel) public void SetMeta(string title, string channel)
+1 -1
View File
@@ -11,7 +11,7 @@
RequestedTheme="Dark"> RequestedTheme="Dark">
<Grid Background="White" Name="grid"> <Grid Background="White" Name="grid">
<MediaElement Name="videoSource" AreTransportControlsEnabled="True" PosterSource="ms-appx:///Assets/videoThumbSample.png" CurrentStateChanged="VideoSource_CurrentStateChanged" MediaOpened="VideoSource_MediaOpened" VolumeChanged="VideoSource_VolumeChanged"> <MediaElement Name="videoSource" MarkerReached="VideoSource_MarkerReached" AreTransportControlsEnabled="True" PosterSource="ms-appx:///Assets/videoThumbSample.png" CurrentStateChanged="VideoSource_CurrentStateChanged" MediaOpened="VideoSource_MediaOpened" VolumeChanged="VideoSource_VolumeChanged">
<MediaElement.TransportControls> <MediaElement.TransportControls>
<foxtube:PlayerControls IsCompactOverlayButtonVisible="True" IsCompactOverlayEnabled="True" <foxtube:PlayerControls IsCompactOverlayButtonVisible="True" IsCompactOverlayEnabled="True"
IsFullWindowButtonVisible="True" IsFullWindowEnabled="True" IsFullWindowButtonVisible="True" IsFullWindowEnabled="True"
@@ -49,6 +49,8 @@ namespace FoxTube
if (item.Snippet.LiveBroadcastContent == "none") if (item.Snippet.LiveBroadcastContent == "none")
{ {
InitializeContols(); InitializeContols();
if (Methods.GetDuration(item.ContentDetails.Duration).TotalMinutes > 5)
videoSource.Markers.Add(new Windows.UI.Xaml.Media.TimelineMarker { Time = Methods.GetDuration(item.ContentDetails.Duration) - TimeSpan.FromMinutes(1) });
Controls.SetQualities(await new YoutubeClient().GetVideoMediaStreamInfosAsync(item.Id)); Controls.SetQualities(await new YoutubeClient().GetVideoMediaStreamInfosAsync(item.Id));
Controls.SetCaptions(await new YoutubeClient().GetVideoClosedCaptionTrackInfosAsync(item.Id)); Controls.SetCaptions(await new YoutubeClient().GetVideoClosedCaptionTrackInfosAsync(item.Id));
} }
@@ -230,5 +232,10 @@ namespace FoxTube
audioSource.Volume = videoSource.Volume; audioSource.Volume = videoSource.Volume;
SettingsStorage.Volume = videoSource.Volume; SettingsStorage.Volume = videoSource.Volume;
} }
private void VideoSource_MarkerReached(object sender, Windows.UI.Xaml.Media.TimelineMarkerRoutedEventArgs e)
{
Controls.Advert.PushAdvert();
}
} }
} }
+7
View File
@@ -116,6 +116,9 @@
<Compile Include="Controls\Adverts\CommentAdvert.xaml.cs"> <Compile Include="Controls\Adverts\CommentAdvert.xaml.cs">
<DependentUpon>CommentAdvert.xaml</DependentUpon> <DependentUpon>CommentAdvert.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Controls\Adverts\PlayerAdvert.xaml.cs">
<DependentUpon>PlayerAdvert.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\ChannelCard.xaml.cs"> <Compile Include="Controls\ChannelCard.xaml.cs">
<DependentUpon>ChannelCard.xaml</DependentUpon> <DependentUpon>ChannelCard.xaml</DependentUpon>
</Compile> </Compile>
@@ -289,6 +292,10 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="Controls\Adverts\PlayerAdvert.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Controls\ChannelCard.xaml"> <Page Include="Controls\ChannelCard.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
-1
View File
@@ -6,7 +6,6 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:pages="using:FoxTube.Pages" xmlns:pages="using:FoxTube.Pages"
xmlns:controls="using:FoxTube.Controls"
mc:Ignorable="d"> mc:Ignorable="d">
<Grid Name="grid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" SizeChanged="grid_SizeChanged"> <Grid Name="grid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" SizeChanged="grid_SizeChanged">
+16 -8
View File
@@ -2,7 +2,8 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:FoxTube" xmlns:local="using:FoxTube"
xmlns:controls="using:FoxTube.Controls"> xmlns:controls="using:FoxTube.Controls"
xmlns:adverts="using:FoxTube.Controls.Adverts">
<Style TargetType="local:PlayerControls"> <Style TargetType="local:PlayerControls">
<Setter Property="IsTabStop" Value="False" /> <Setter Property="IsTabStop" Value="False" />
@@ -13,14 +14,12 @@
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>
<ControlTemplate TargetType="local:PlayerControls"> <ControlTemplate TargetType="local:PlayerControls">
<Grid Background="Transparent"> <Grid Background="Transparent" Name="root">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.Resources> <Grid.Resources>
<Style TargetType="Button" BasedOn="{StaticResource ButtonRevealStyle}">
<Setter Property="Width" Value="50"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Background" Value="Transparent"/>
</Style>
<Style x:Key="AppBarButtonStyle" TargetType="AppBarButton"> <Style x:Key="AppBarButtonStyle" TargetType="AppBarButton">
<Setter Property="Width" Value="{ThemeResource MTCMediaButtonWidth}" /> <Setter Property="Width" Value="{ThemeResource MTCMediaButtonWidth}" />
<Setter Property="Height" Value="{ThemeResource MTCMediaButtonHeight}" /> <Setter Property="Height" Value="{ThemeResource MTCMediaButtonHeight}" />
@@ -483,6 +482,13 @@
<Border x:Name="Border"> <Border x:Name="Border">
<Grid x:Name="ControlPanelGrid"> <Grid x:Name="ControlPanelGrid">
<Grid.Resources>
<Style TargetType="Button" BasedOn="{StaticResource ButtonRevealStyle}">
<Setter Property="Width" Value="50"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Background" Value="Transparent"/>
</Style>
</Grid.Resources>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="auto"/> <RowDefinition Height="auto"/>
<RowDefinition/> <RowDefinition/>
@@ -628,6 +634,8 @@
</Grid> </Grid>
</Border> </Border>
<controls:LiveCaptions Visibility="Collapsed" x:Name="captions"/> <controls:LiveCaptions Visibility="Collapsed" x:Name="captions"/>
<adverts:PlayerAdvert Grid.Row="1" x:Name="ad" VerticalAlignment="Bottom"/>
</Grid> </Grid>
</ControlTemplate> </ControlTemplate>
</Setter.Value> </Setter.Value>