Archived
1
0
This repository has been archived on 2026-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FoxTube/FoxTube/Controls/LiveCaptions.xaml.cs
T
Michael Gordeev cc117a161c Closed captions
2018-09-08 23:49:56 +03:00

89 lines
2.6 KiB
C#

using FoxTube.Classes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
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
namespace FoxTube.Controls
{
public sealed partial class LiveCaptions : UserControl
{
public MediaElement Player { get; set; }
DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(10) };
List<Caption> captions = new List<Caption>();
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();
}
}
}