forked from RobG66/Gamelist-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoJukeBox.xaml.cs
More file actions
285 lines (245 loc) · 8.87 KB
/
VideoJukeBox.xaml.cs
File metadata and controls
285 lines (245 loc) · 8.87 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using GamelistManager.classes;
using LibVLCSharp.Shared;
using LibVLCSharp.WPF;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace GamelistManager
{
public partial class JukeBoxWindow : Window
{
private readonly string[] _filePaths;
private readonly Random _random;
private LibVLC _libVLC = null!;
private LibVLCSharp.Shared.MediaPlayer _mediaPlayer = null!;
private int _currentIndex;
private bool _isPaused = false;
private int _lastIndex;
private bool _randomPlayback;
private bool _isVideo;
public JukeBoxWindow(string[] paths, bool boolValue)
{
InitializeComponent();
_random = new Random();
_filePaths = paths;
_randomPlayback = false;
_currentIndex = 0;
_lastIndex = paths.Length - 1;
_isVideo = boolValue;
var fileNames = Array.ConvertAll(_filePaths, Path.GetFileName);
comboBox_CurrentTrack.ItemsSource = fileNames;
InitializeVLC();
TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
string systemName = textInfo.ToTitleCase(SharedData.CurrentSystem);
string typeOfJukebox = _isVideo ? "Video" : "Music";
this.Title = $"{systemName} {typeOfJukebox} Jukebox";
PlayCurrentVideo(0);
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
AnimateButton(button);
// slight delay
await Task.Delay(250);
// Process the method based on button name
switch (button.Name)
{
case "button_Previous":
PlayNextVideo(-1);
break;
case "button_Pause":
PauseVideo();
break;
case "button_Stop":
StopVideo();
break;
case "button_Play":
PlayVideoOrResume();
break;
case "button_Next":
PlayNextVideo(1);
break;
}
}
}
private void InitializeVLC()
{
var vlcOptions = new List<string>();
if (!_isVideo)
{
// string selectedVisualization = "Goom"; // Default to Goom
vlcOptions.Add("--audio-visual=visual");
vlcOptions.Add("--effect-width=50");
vlcOptions.Add("--effect-height=50");
vlcOptions.Add("--effect-list=spectrometer"); var libVlcDirectory = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "vlc"));
vlcOptions.Add($"--plugin-path={libVlcDirectory}");
}
vlcOptions.Add("--network-caching=3000");
vlcOptions.Add("--file-caching=3000");
//vlcOptions.Add("--video-title-position=8");
var options = vlcOptions.ToArray();
_libVLC = new LibVLC(options);
_mediaPlayer = new LibVLCSharp.Shared.MediaPlayer(_libVLC);
_mediaPlayer.EndReached += MediaPlayer_EndReached!;
VideoView.MediaPlayer = _mediaPlayer;
}
private void PlayVideo(string fileName)
{
var media = new Media(_libVLC, new Uri(fileName));
_mediaPlayer.Media = media;
_mediaPlayer.Play();
UpdateButtonStates(isPlaying: true);
}
private void MediaPlayer_EndReached(object sender, EventArgs e)
{
Task.Run(() =>
{
Application.Current.Dispatcher.Invoke(() =>
{
try
{
_mediaPlayer.Stop();
PlayNextVideo(1);
}
catch
{
// Handle exceptions as needed
}
});
});
}
private void PlayNextVideo(int modifier)
{
if (_randomPlayback)
{
_currentIndex = _random.Next(0, _lastIndex + 1);
}
else
{
_currentIndex += modifier;
if (_currentIndex > _lastIndex || _currentIndex < 0)
{
_currentIndex = 0;
}
}
PlayCurrentVideo(_currentIndex);
}
private void PlayCurrentVideo(int index)
{
string fileName = _filePaths[index];
comboBox_CurrentTrack.SelectedIndex = index;
PlayVideo(fileName);
}
private void PauseVideo()
{
if (_mediaPlayer.IsPlaying)
{
_mediaPlayer.Pause();
_isPaused = true;
UpdateButtonStates(isPlaying: false);
}
}
private void StopVideo()
{
_mediaPlayer?.Stop();
_isPaused = false;
UpdateButtonStates(isPlaying: false);
}
private void PlayVideoOrResume()
{
if (_mediaPlayer.IsPlaying)
{
return;
}
if (_isPaused)
{
_mediaPlayer.Play();
}
else
{
PlayCurrentVideo(_currentIndex);
}
_isPaused = false;
UpdateButtonStates(isPlaying: true);
}
private void UpdateButtonStates(bool isPlaying)
{
button_Play.IsEnabled = !isPlaying;
button_Pause.IsEnabled = isPlaying;
button_Stop.IsEnabled = isPlaying;
button_Next.IsEnabled = isPlaying;
button_Previous.IsEnabled = isPlaying;
}
protected override void OnClosed(EventArgs e)
{
if (_mediaPlayer.IsPlaying || _isPaused)
{
_mediaPlayer.Stop();
}
_mediaPlayer.Dispose();
_libVLC.Dispose();
base.OnClosed(e);
}
private void checkBox_Randomize_Click(object sender, RoutedEventArgs e)
{
_randomPlayback = checkBox_Randomize.IsChecked == true;
}
private void AnimateButton(Button button)
{
// Create and configure the animation
Storyboard storyboard = new Storyboard();
// ScaleX Animation
DoubleAnimation scaleXAnimation = new DoubleAnimation
{
To = 0.9,
Duration = new Duration(TimeSpan.FromSeconds(0.08)),
AutoReverse = true
};
Storyboard.SetTarget(scaleXAnimation, button);
Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath("(Button.RenderTransform).(ScaleTransform.ScaleX)"));
storyboard.Children.Add(scaleXAnimation);
// ScaleY Animation
DoubleAnimation scaleYAnimation = new DoubleAnimation
{
To = 0.9,
Duration = new Duration(TimeSpan.FromSeconds(0.08)),
AutoReverse = true
};
Storyboard.SetTarget(scaleYAnimation, button);
Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath("(Button.RenderTransform).(ScaleTransform.ScaleY)"));
storyboard.Children.Add(scaleYAnimation);
// Set a ScaleTransform as RenderTransform of the button
button.RenderTransform = new ScaleTransform();
// Start the animation
storyboard.Begin();
}
private void comboBox_CurrentTrack_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = comboBox_CurrentTrack.SelectedIndex;
PlayCurrentVideo(index);
}
private void button_Playlist_Click_1(object sender, RoutedEventArgs e)
{
// The animate button is a method because I had difficulty with it
// in a style. Changing the mediaplayer file would interfere with the
// button animations. This way, I can do the animation and then other stuff
// in order. Maybe there's another solution, I tried several.
if (sender is Button button)
{
AnimateButton(button);
}
if (stackPanel_FileSelector.Visibility == Visibility.Visible)
{
stackPanel_FileSelector.Visibility = Visibility.Collapsed;
}
else
{
stackPanel_FileSelector.Visibility = Visibility.Visible;
}
}
}
}