forked from ashpynov/PlayniteSound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaElementsMonitor.cs
137 lines (119 loc) · 5.15 KB
/
MediaElementsMonitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Playnite.SDK;
using PlayniteSounds.Models;
using System.Threading;
using System;
using System.Runtime.Serialization.Formatters;
using System.Windows.Input;
namespace PlayniteSounds
{
class MediaElementsMonitor
{
private static IPlayniteAPI playniteApi;
private static PlayniteSoundsSettings settings;
static private DispatcherTimer timer;
class WeakTargetComparer : IEqualityComparer<WeakReference>
{
public bool Equals(WeakReference b1, WeakReference b2)
{
return ReferenceEquals(b1.Target, b2.Target);
}
public int GetHashCode(WeakReference r)
{
var Target = r.Target;
return Target != null ? r.Target.GetHashCode() : 0;
}
}
readonly static private Dictionary<WeakReference, TimeSpan> mediaElementPositions = new Dictionary<WeakReference, TimeSpan>(new WeakTargetComparer());
private static readonly ILogger logger = LogManager.GetLogger();
static public void Attach(IPlayniteAPI api, PlayniteSoundsSettings settings)
{
playniteApi = api;
MediaElementsMonitor.settings = settings;
EventManager.RegisterClassHandler(typeof(MediaElement), MediaElement.MediaOpenedEvent, new RoutedEventHandler(MediaElement_Opened));
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(10);
timer.Tick += Timer_Tick;
}
public static List<MediaElement> GetAllMediaElements(DependencyObject parent)
{
var list = new List<MediaElement>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is MediaElement mediaElement
&& mediaElement.NaturalVideoWidth != 0
&& mediaElement.NaturalVideoHeight != 0
&& mediaElement.HasAudio
&& mediaElement.IsVisible
&& (mediaElement.LoadedBehavior == MediaState.Manual || mediaElement.Position < mediaElement.NaturalDuration)
)
{
list.Add(mediaElement);
}
list.AddRange(GetAllMediaElements(child));
}
return list;
}
static private void Timer_Tick(object sender, EventArgs e)
{
bool someIsPlaying = false;
List<MediaElement> mediaElements = new List<MediaElement>();
foreach (Window w in Application.Current.Windows)
{
mediaElements.AddRange(GetAllMediaElements(w));
}
foreach (var mediaElement in mediaElements)
{
if (! mediaElementPositions.ContainsKey(new WeakReference(mediaElement)))
mediaElementPositions[new WeakReference(mediaElement)] = mediaElement.Position;
}
List<MediaElement> playing = new List<MediaElement>();
var keysToRemove = new List<WeakReference>();
var keys = new List<WeakReference>();
keys.AddRange(mediaElementPositions.Keys);
foreach (var mediaElementReference in keys)
{
if (!(mediaElementReference.Target is MediaElement mediaElement))
{
// The MediaElement has been collected, remove it from the dictionary
keysToRemove.Add(mediaElementReference);
}
else if (mediaElementPositions[mediaElementReference] != mediaElement.Position)
{
// The position has changed, update the position in the dictionary
mediaElementPositions[mediaElementReference] = mediaElement.Position;
someIsPlaying |= mediaElement.HasAudio && !mediaElement.IsMuted && mediaElement.Volume > 0;
}
else if(!mediaElement.IsVisible || mediaElement.LoadedBehavior!=MediaState.Manual && mediaElement.Position>=mediaElement.NaturalDuration)
{
keysToRemove.Add(mediaElementReference);
}
}
if (settings.VideoIsPlaying != someIsPlaying)
{
settings.VideoIsPlaying = someIsPlaying;
}
foreach (var key in keysToRemove)
{
mediaElementPositions.Remove(key);
}
if (mediaElementPositions.Count == 0)
{
timer.Stop();
settings.VideoIsPlaying = false;
}
}
static private void MediaElement_Opened(object sender, RoutedEventArgs e)
{
Timer_Tick(sender, e);
timer.Start();
}
}
}