diff --git a/FoxTube/Classes/Caption.cs b/FoxTube/Classes/Caption.cs new file mode 100644 index 0000000..14567bc --- /dev/null +++ b/FoxTube/Classes/Caption.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FoxTube.Classes +{ + public class Caption + { + public TimeSpan Start { get; private set; } + public TimeSpan Duration { get; private set; } + public TimeSpan End + { + get { return Start + Duration; } + } + public string Text { get; private set; } + + public Caption(int startTime, int duration, string text) + { + Start = TimeSpan.FromMilliseconds(startTime); + Duration = TimeSpan.FromMilliseconds(duration); + Text = text; + } + } +} diff --git a/FoxTube/Controls/LiveCaptions.xaml b/FoxTube/Controls/LiveCaptions.xaml index a65d4f8..20c54fb 100644 --- a/FoxTube/Controls/LiveCaptions.xaml +++ b/FoxTube/Controls/LiveCaptions.xaml @@ -8,9 +8,9 @@ mc:Ignorable="d" HorizontalAlignment="Center" VerticalAlignment="Bottom" - Margin="0,75"> + Margin="0,55"> - + diff --git a/FoxTube/Controls/LiveCaptions.xaml.cs b/FoxTube/Controls/LiveCaptions.xaml.cs index e468565..7231128 100644 --- a/FoxTube/Controls/LiveCaptions.xaml.cs +++ b/FoxTube/Controls/LiveCaptions.xaml.cs @@ -1,4 +1,5 @@ -using System; +using FoxTube.Classes; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -12,6 +13,8 @@ using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; +using System.Xml; +using System.Diagnostics; // The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 @@ -19,9 +22,67 @@ namespace FoxTube.Controls { public sealed partial class LiveCaptions : UserControl { + public MediaElement Player { get; set; } + DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(10) }; + List captions = new List(); + Caption currentCaption = null; + public LiveCaptions() { this.InitializeComponent(); + timer.Tick += UpdateCaption; + } + + private void UpdateCaption(object sender, object e) + { + TimeSpan currentPosition = Player.Position; + + bool found = false; + captions.ForEach((x) => + { + if (Player.Position >= x.Start && Player.Position <= x.End) + { + currentCaption = x; + text.Text = currentCaption.Text; + Visibility = Visibility.Visible; + found = true; + } + }); + + if (!found) + { + currentCaption = null; + Visibility = Visibility.Collapsed; + } + } + + public void Initialize(string source) + { + XmlDocument doc = new XmlDocument(); + doc.Load(source); + + foreach (XmlElement i in doc["timedtext"]["body"].ChildNodes) + captions.Add(new Caption(int.Parse(i.GetAttribute("t")), int.Parse(i.GetAttribute("d")), i.InnerText)); + + captions.ForEach((x) => + { + if(Player.Position > x.Start && Player.Position < x.End) + { + currentCaption = x; + text.Text = currentCaption.Text; + Visibility = Visibility.Visible; + } + }); + + timer.Start(); + } + + public void Close() + { + captions.Clear(); + currentCaption = null; + Visibility = Visibility.Collapsed; + timer.Stop(); } } } diff --git a/FoxTube/Controls/VideoPlayer.xaml b/FoxTube/Controls/VideoPlayer.xaml index f9bafc7..70cb15c 100644 --- a/FoxTube/Controls/VideoPlayer.xaml +++ b/FoxTube/Controls/VideoPlayer.xaml @@ -19,7 +19,7 @@ - + @@ -36,7 +36,7 @@ - - - - +