-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
430 lines (342 loc) · 14.8 KB
/
MainPage.xaml.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.Storage.Pickers;
using Windows.Storage;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;
using Soft_Walkman.Models;
using Windows.ApplicationModel.Core;
using System.IO;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace Soft_Walkman
{
/// <summary>
/// The Main and only page for the Walkman.
/// </summary>
///
public sealed partial class MainPage : Page
{
private Models.CassetteTape cassetteTape;
private Models.Walkman walkman;
private DispatcherTimer cassetteNameScrollTimer;
const double DEFAULT_MEDIAPLAYER_VOLUME_LEVEL = 5.0;
const int DEFAULT_FF_RR_VALUE = 3;
public MainPage()
{
this.InitializeComponent();
var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
coreTitleBar.ExtendViewIntoTitleBar = false; // Don't hide TitleBar
this.cassetteTapeGif.AutoPlay = false;
this.cassetteTrackListButton.IsEnabled = false;
this.cassetteAlbumArtButton.IsEnabled = false;
volumeSlider.Value = DEFAULT_MEDIAPLAYER_VOLUME_LEVEL;
Application.Current.Suspending += new SuspendingEventHandler(App_Suspending);
Application.Current.UnhandledException += new UnhandledExceptionEventHandler(App_UnhandledException);
ResumeTape();
}
private async void ResumeTape()
{
CassetteTapeState tapeState = new CassetteTapeState();
if (tapeState.GetTapeSaved())
{
/*
* I f music directory moved, or external drive not mounted anymore reset Tape state.
*
* Also we can't run the Directory check from the UI thread:
*/
bool tapePathExists = await Task.Run(() => { return Directory.Exists(tapeState.GetTapePath()); });
if (!tapePathExists)
{
tapeState.ClearTapeState();
return;
}
DisableFlyoutButtons();
EnableUIButtons(false);
walkman = new Models.Walkman();
walkman.Volume(DEFAULT_MEDIAPLAYER_VOLUME_LEVEL);
cassetteTape = new Models.CassetteTape();
cassetteTape.DirPath = await StorageFolder.GetFolderFromPathAsync(tapeState.GetTapePath());
cassetteTitleLabel.Text = cassetteTape.Title;
await walkman.LoadCassetteTape(cassetteTape);
walkman.MediaPlaybackList.MoveTo(tapeState.GetTapeTrackIndex());
walkman.MediaPlayer.PlaybackSession.Position = tapeState.GetTapePostion();
CassetteTrackListView.ItemsSource = await cassetteTape.Tracks();
cassetteTrackListButton.IsEnabled = true;
await DisplayAlbumArtAsync();
EnableUIButtons(true);
}
}
private void App_Suspending(Object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
if (walkman != null && cassetteTape != null)
{
Debug.WriteLine("Saving tape state on exit...");
CassetteTapeState cts = new CassetteTapeState(walkman, cassetteTape);
cts.SaveTapeState();
}
}
private void App_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
{
CassetteTapeState cts = new CassetteTapeState();
cts.ClearTapeState();
walkman.Reset();
}
private async void OpenCassetteButton_ClickAsync(object sender, RoutedEventArgs e)
{
// Eject tape. Clear cassette and reset walkman state.
if (cassetteTape != null && walkman != null)
{
CassetteTapeState tapeState = new CassetteTapeState();
tapeState.ClearTapeState();
walkman.PlaySound("eject");
walkman.Reset();
cassetteTapeGif.Stop();
DisableFlyoutButtons();
if (cassetteNameScrollTimer != null)
{
cassetteNameScrollTimer.Stop();
}
cassetteTitleLabel.Text = string.Empty;
}
walkman = new Models.Walkman();
walkman.Volume(DEFAULT_MEDIAPLAYER_VOLUME_LEVEL);
walkman.PlaySound("open");
FolderPicker fp = new FolderPicker
{
CommitButtonText = "Load Cassette Tape",
SuggestedStartLocation = PickerLocationId.MusicLibrary,
ViewMode = PickerViewMode.Thumbnail
};
fp.FileTypeFilter.Add(".");
StorageFolder directoryPath = await fp.PickSingleFolderAsync();
if (directoryPath != null)
{
string _ = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(directoryPath);
cassetteTape = new Models.CassetteTape();
cassetteTape.DirPath = directoryPath;
cassetteTitleLabel.Text = cassetteTape.Title;
await walkman.LoadCassetteTape(cassetteTape);
// Populate Track listview:
CassetteTrackListView.ItemsSource = await cassetteTape.Tracks();
cassetteTrackListButton.IsEnabled = true;
await DisplayAlbumArtAsync();
EnableUIButtons(false);
}
EnableUIButtons(true);
}
private void DisableFlyoutButtons()
{
AlbumArtImage.Source = null;
cassetteAlbumArtButton.IsEnabled = false;
CassetteTrackListView.ItemsSource = null;
cassetteTrackListButton.IsEnabled = false;
}
private async Task DisplayAlbumArtAsync()
{
string albumCoverPath = await cassetteTape.FindCoverArt();
if (albumCoverPath != null && albumCoverPath.Length > 0 && albumCoverPath != "" && albumCoverPath != " ")
{
StorageFile coverStorageFile = await StorageFile.GetFileFromPathAsync(albumCoverPath);
using (var stream = await coverStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapImage bitmap = new BitmapImage();
await bitmap.SetSourceAsync(stream);
AlbumArtImage.Source = bitmap;
}
this.cassetteAlbumArtButton.IsEnabled = true;
}
}
private void EnableUIButtons(bool enabled_state)
{
openButton.IsEnabled = enabled_state;
fastFowardButton.IsEnabled = enabled_state;
rewindButton.IsEnabled = enabled_state;
pauseButton.IsEnabled = enabled_state;
stopButton.IsEnabled = enabled_state;
playButton.IsEnabled = enabled_state;
}
private async void Play_ClickAsync(object sender, RoutedEventArgs e)
{
Debug.WriteLine("running Play_ClickAsync");
if (cassetteTape == null || walkman == null)
{
ErrorDialog("Have you loaded your tape yet?", "Click the open button(+) to load your cassette tape.");
}
else if (cassetteTape != null && await cassetteTape.MediaSizeAsync() > 0)
{
//
// Make sure the walkman system sounds can't play while audio is already playing.
//
if (walkman.MediaPlayer.PlaybackSession.PlaybackState != Windows.Media.Playback.MediaPlaybackState.Playing)
{
walkman.PlaySound("play");
cassetteTapeGif.Play();
ScrollTapeName();
}
walkman.ChangeLightIndicator(lightIndicator, "play");
EnableUIButtons(false);
// Give some time for the Walkman system sound to play before actually playing the tape.
var delay = Task.Delay(2750);
await delay;
while (delay.Status == TaskStatus.Running)
{
EnableUIButtons(false);
}
if (delay.Status == TaskStatus.RanToCompletion)
{
walkman.Play();
EnableUIButtons(true);
}
else
{
EnableUIButtons(true);
}
}
}
private void fastFowardButton_Click(object sender, RoutedEventArgs e)
{
if (walkman != null && cassetteTape != null)
{
walkman.FastForward(DEFAULT_FF_RR_VALUE);
}
}
private void RewindButton_Click(object sender, RoutedEventArgs e)
{
if (walkman != null && cassetteTape != null)
{
walkman.Rewind(DEFAULT_FF_RR_VALUE);
}
}
private void PauseButton_Click(object sender, RoutedEventArgs e)
{
if (walkman != null && cassetteNameScrollTimer != null)
{
walkman.ChangeLightIndicator(lightIndicator, "pause");
walkman.Pause();
cassetteNameScrollTimer.Stop();
this.cassetteTapeGif.Stop();
CassetteTapeState tapeState = new CassetteTapeState(walkman, cassetteTape);
tapeState.SaveTapeState();
}
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
if (walkman != null && cassetteNameScrollTimer != null)
{
walkman.ChangeLightIndicator(lightIndicator, "stop");
cassetteTape = null;
walkman.Reset();
cassetteTapeGif.Stop();
cassetteNameScrollTimer.Stop();
cassetteTitleLabel.Text = string.Empty;
DisableFlyoutButtons();
CassetteTapeState tapeState = new CassetteTapeState();
tapeState.ClearTapeState();
}
}
private void ChangeWalkmanVolume(object sender, RangeBaseValueChangedEventArgs e)
{
if (walkman != null && cassetteTape != null)
{
walkman.Volume(volumeSlider.Value);
}
}
private async void ErrorDialog(string title, string content)
{
ContentDialog cassetteTapeLoadErrorDialog = new ContentDialog
{
Title = title,
Content = content,
CloseButtonText = "Ok"
};
await cassetteTapeLoadErrorDialog.ShowAsync();
}
private void Tape_DragOver(object sender, DragEventArgs e)
{
e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
}
private async void Tape_DragDrop(object sender, DragEventArgs e)
{
if (cassetteTape != null)
{
return;
}
if (e.DataView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.StorageItems))
{
var items = await e.DataView.GetStorageItemsAsync();
if (items.Count > 0)
{
walkman = new Models.Walkman();
walkman.Volume(DEFAULT_MEDIAPLAYER_VOLUME_LEVEL);
walkman.PlaySound("open");
StorageFolder directoryPath = (StorageFolder)items[0];
if (directoryPath != null)
{
string _ = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(directoryPath);
cassetteTape = new Models.CassetteTape();
cassetteTape.DirPath = directoryPath;
cassetteTitleLabel.Text = cassetteTape.Title;
await walkman.LoadCassetteTape(cassetteTape);
// Populate Track listview:
CassetteTrackListView.ItemsSource = await cassetteTape.Tracks();
cassetteTrackListButton.IsEnabled = true;
await DisplayAlbumArtAsync();
}
}
}
}
private void ScrollTapeName()
{
const int TICK_COUNT = 100;
const int MAX_LABEL_LENGTH = 23;
cassetteNameScrollTimer = new DispatcherTimer();
if ( (cassetteTitleLabel.Text.Length - 2 ) >= MAX_LABEL_LENGTH)
{
cassetteNameScrollTimer.Tick += async (ss, ee) =>
{
if (cassetteNameScrollTimer.Interval.Ticks == TICK_COUNT)
{
//each time set the offset to scrollviewer.HorizontalOffset + 5
scrollviewer.ChangeView(scrollviewer.HorizontalOffset + 0.5, null, null, true);
//if the scrollviewer scrolls to the end, scroll it back to the start.
if (scrollviewer.HorizontalOffset == scrollviewer.ScrollableWidth)
{
await Task.Delay(700);
scrollviewer.ChangeView(-1, null, null, true);
}
}
};
cassetteNameScrollTimer.Interval = new TimeSpan(TICK_COUNT);
cassetteNameScrollTimer.Start();
}
}
private void TapeNameScrollViewer_Unloaded(object sender, RoutedEventArgs e)
{
cassetteNameScrollTimer.Stop();
}
private void fastFowardButton_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (walkman != null && cassetteTape != null)
{
walkman.FastForward(1);
}
}
private void VolumeIncreased_Invoked(Windows.UI.Xaml.Input.KeyboardAccelerator sender, Windows.UI.Xaml.Input.KeyboardAcceleratorInvokedEventArgs args)
{
if (walkman != null && cassetteTape != null)
{
walkman.Volume(volumeSlider.Value += 0.5);
}
}
private void VolumeDecreased_Invoked(Windows.UI.Xaml.Input.KeyboardAccelerator sender, Windows.UI.Xaml.Input.KeyboardAcceleratorInvokedEventArgs args)
{
if (walkman != null && cassetteTape != null)
{
walkman.Volume(volumeSlider.Value -= 0.5);
}
}
}
}