89 lines
2.6 KiB
C#
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();
|
|
}
|
|
}
|
|
}
|