Skip to content

Instantly share code, notes, and snippets.

@Nielk1
Created May 8, 2017 19:07
Show Gist options
  • Save Nielk1/173427d27ccdc63a8953e3a9874d3838 to your computer and use it in GitHub Desktop.
Save Nielk1/173427d27ccdc63a8953e3a9874d3838 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Unbroken.LaunchBox.Plugins;
using Unbroken.LaunchBox.Plugins.Data;
using Unbroken.LaunchBox.Wpf.BigBox.Controls;
namespace Nielk1Controls
{
public class RandomVideoControl : Grid
{
//static RandomVideoControl()
//{
// DefaultStyleKeyProperty.OverrideMetadata(typeof(RandomVideoControl), new FrameworkPropertyMetadata(typeof(RandomVideoControl)));
//}
private string _VideoDirPath;
public string VideoDirPath
{
get
{
return _VideoDirPath;
}
set
{
_VideoDirPath = value;
PlayRandomVideo();
}
}
private object SubControlLock = new object();
private VideoControl SubControl;
private int currentVideo = -1;
private Random Random = new Random();
public RandomVideoControl()
{
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
InitializeComponent();
}
}
protected void InitializeComponent()
{
// I probably don't need to lock in the constructor but let's do it anyway
lock (SubControlLock)
{
SubControl = new VideoControl();
Grid.SetRow(SubControl, 0);
Grid.SetColumn(SubControl, 0);
this.Children.Add(SubControl);
}
this.IsVisibleChanged += RandomVideoControl_IsVisibleChanged;
}
private void RandomVideoControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (IsVisible)
{
this.Dispatcher.BeginInvoke(new Action(() =>
{
lock (SubControlLock)
{
this.Children.Remove(SubControl);
SubControl.Dispose();
SubControl = new VideoControl();
SubControl.AspectRatio4X3 = _AspectRatio4X3;
SubControl.StretchVideo = _StretchVideo;
SubControl.CenterVideo = _CenterVideo;
Grid.SetRow(SubControl, 0);
Grid.SetColumn(SubControl, 0);
this.Children.Add(SubControl);
// double locks on the same object should work passivly and safely
PlayRandomVideo();
}
}));
}
}
private void PlayRandomVideo()
{
lock (SubControlLock)
{
Uri uri = new Uri(_VideoDirPath);
string[] videos = Directory.GetFiles(uri.LocalPath.TrimStart('/'), @"*.mp4");
if (currentVideo >= 0)
{
if (videos.Length > 1)
{
int index = Random.Next(0, videos.Length - 1);
if (index >= currentVideo) index++;
currentVideo = index;
SubControl.PlayVlcVideo(new Uri(_VideoDirPath.TrimEnd('/') + @"/" + System.IO.Path.GetFileName(videos[index])));
}
}
else if (videos.Length > 0)
{
int index = Random.Next(0, videos.Length);
SubControl.PlayVlcVideo(new Uri(_VideoDirPath.TrimEnd('/') + @"/" + System.IO.Path.GetFileName(videos[index])));
}
}
}
//public Uri VideoPath { get { return SubControl.VideoPath; } set { SubControl.VideoPath = value; } }
//public string VideoPathString { get { return SubControl.VideoPathString; } set { SubControl.VideoPathString = value; } }
private bool _AspectRatio4X3;
public bool AspectRatio4X3 { get { lock (SubControlLock) { return SubControl.AspectRatio4X3; } } set { lock (SubControlLock) { SubControl.AspectRatio4X3 = _AspectRatio4X3 = value; } } }
private bool _StretchVideo;
public bool StretchVideo { get { lock (SubControlLock) { return SubControl.StretchVideo; } } set { lock (SubControlLock) { SubControl.StretchVideo = _StretchVideo = value; } } }
private bool _CenterVideo;
public bool CenterVideo { get { lock (SubControlLock) { return SubControl.CenterVideo; } } set { lock (SubControlLock) { SubControl.CenterVideo = _CenterVideo = value; } } }
public void Dispose()
{
lock (SubControlLock) { SubControl.Dispose(); }
}
public void Mute()
{
lock (SubControlLock) { SubControl.Mute(); }
}
public void PauseVideo()
{
lock (SubControlLock) { SubControl.PauseVideo(); }
}
public void PlayVlcVideo(Uri path, int tries = 0)
{
lock (SubControlLock) { SubControl.PlayVlcVideo(path, tries); }
}
public void ResumeVideo()
{
lock (SubControlLock) { SubControl.ResumeVideo(); }
}
protected virtual void Dispose(bool disposing) { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment