Skip to content

Commit c0bcc2c

Browse files
authored
Merge pull request #31 from kubbit98/feature13
add audio support
2 parents 68d9baf + 3e2d0d7 commit c0bcc2c

4 files changed

Lines changed: 69 additions & 11 deletions

File tree

Data/File.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ public class File : ICloneable
44
{
55
private static readonly string[] s_videoExtensions = ["flv", "m3u8", "ts", "3gp", "qt", "wmv", "m4v", "mpg", "asf", "ogv", "oga", "ogx", "ogg", "spx", "webm", "avi", "mov", "mp4", "m4a", "m4p", "m4b", "m4r"]; //not sure if all of them works
66
private static readonly string[] s_photoExtensions = ["gif", "jpeg", "jpg", "png", "webp", "apng", "avif"]; //supported by <img> tag
7+
private static readonly string[] s_audioExtensions = ["mp3", "wav", "ogg"]; //supported by <audio> tag
78
private static readonly string[] s_textExtensions = ["txt", "csv", "log", "md", "html", "js", "css"];
89
private static readonly string[] s_pdfExtensions = ["pdf"];
910
public enum FileTypeEnum
1011
{
11-
Video, Photo, Text, PDF, NotSupported
12+
Video, Photo, Audio, Text, PDF, NotSupported
1213
}
1314
public enum ThumbnailEnum
1415
{
@@ -76,6 +77,7 @@ public FileTypeEnum FileType
7677
{
7778
if (s_photoExtensions.Contains(Extension.ToLower())) return FileTypeEnum.Photo;
7879
else if (s_videoExtensions.Contains(Extension.ToLower())) return FileTypeEnum.Video;
80+
else if (s_audioExtensions.Contains(Extension.ToLower())) return FileTypeEnum.Audio;
7981
else if (s_pdfExtensions.Contains(Extension.ToLower())) return FileTypeEnum.PDF;
8082
else if (s_textExtensions.Contains(Extension.ToLower())) return FileTypeEnum.Text;
8183
else return FileTypeEnum.NotSupported;

Pages/Index.razor

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@
163163
IsPrevPhoto="@(_previousFile?.FileType==File.FileTypeEnum.Photo)"
164164
IsNextPhoto="@(_nextFile?.FileType==File.FileTypeEnum.Photo)" />
165165
break;
166+
case File.FileTypeEnum.Audio:
167+
<audio class="mx-auto w-75" style="max-height: 95vh;" controls autoplay id="audioTagId">
168+
<source src="@_currentFile?.Path">
169+
Your browser does not support the audio tag.
170+
</audio>
171+
break;
166172
case File.FileTypeEnum.PDF:
167173
<embed src="@_currentFile?.Path" style="width:100%;height:95vh;" />
168174
break;
@@ -250,7 +256,7 @@
250256
private string _storagePrefix = string.Empty;
251257

252258
private const bool SHOW_DEBUG_INFO = false;
253-
private const int LOAD_VIDEO_TIME_DELAY = 100;
259+
private const int LOAD_VIDEO_OR_AUDIO_TIME_DELAY = 100;
254260
private const int MOVE_FILE_TIME_DELAY = 500;
255261

256262
private static Regex s_alfanumeralRegex = new Regex(@"^[a-zA-Z0-9]$");
@@ -423,7 +429,7 @@
423429
_nextFile = null;
424430
}
425431
}
426-
ReloadPlayerIfVideo();
432+
ReloadPlayerIfVideoOrAudio();
427433
StateHasChanged();
428434
_isChangingFile = false;
429435
}
@@ -452,7 +458,7 @@
452458
_status = Status.Initialized;
453459
await _PSSWriteStatus();
454460
}
455-
ReloadPlayerIfVideo();
461+
ReloadPlayerIfVideoOrAudio();
456462
StateHasChanged();
457463
await _PSSWriteMyMediaCurrentIndex();
458464
_isChangingFile = false;
@@ -479,7 +485,7 @@
479485
_storagePrefix = await FileService.GetStoragePrefix();
480486
_previousFile = _currentFile = _nextFile = null;
481487
InitSession();
482-
ReloadPlayerIfVideo();
488+
ReloadPlayerIfVideoOrAudio();
483489
StateHasChanged();
484490
}
485491
private async void ShutdownApp()
@@ -511,8 +517,22 @@
511517
await Back();
512518
}
513519
else if (key == "F2") await FileNameInputOpen();
514-
else if (key == " " && null != _currentFile && _currentFile.FileType == File.FileTypeEnum.Photo) await JS.InvokeVoidAsync("toggleModal", "photoModal");
515-
else if (key.Length == 1 && s_alfanumeralRegex.IsMatch(key) && null != _folders)
520+
else if (key == " " && null != _currentFile)
521+
{
522+
switch (_currentFile.FileType)
523+
{
524+
case File.FileTypeEnum.Photo:
525+
await JS.InvokeVoidAsync("toggleModal", "photoModal");
526+
break;
527+
case File.FileTypeEnum.Video:
528+
await JS.InvokeVoidAsync("pauseOrPlayVideo");
529+
break;
530+
case File.FileTypeEnum.Audio:
531+
await JS.InvokeVoidAsync("pauseOrPlayAudio");
532+
break;
533+
}
534+
}
535+
else if (key.Length == 1 && s_alfanumeralRegex.IsMatch(key) && null != _folders)
516536
{
517537
var folder = _folders.Where(f => f.KeyBind == key.First()).FirstOrDefault();
518538
if (null != folder)
@@ -591,13 +611,18 @@
591611
}
592612
}
593613

594-
private async void ReloadPlayerIfVideo()
614+
private async void ReloadPlayerIfVideoOrAudio()
595615
{
596616
if (File.FileTypeEnum.Video == _currentFile?.FileType)
597617
{
598-
await Task.Delay(LOAD_VIDEO_TIME_DELAY);
618+
await Task.Delay(LOAD_VIDEO_OR_AUDIO_TIME_DELAY);
599619
await JS.InvokeVoidAsync("loadVideo");
600620
}
621+
else if (File.FileTypeEnum.Audio == _currentFile?.FileType)
622+
{
623+
await Task.Delay(LOAD_VIDEO_OR_AUDIO_TIME_DELAY);
624+
await JS.InvokeVoidAsync("loadAudio");
625+
}
601626
}
602627
private async void ToggleSidePanel()
603628
{

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ The application has been tested on Windows 11, Linux (Fedora 38) and macOS (Sono
99

1010
As of version 0.10, the application also has support for keyboard shortcuts. Once the application is initialized, the a-z and 0-9 keys can be dynamically assigned to a folder in the settings, the spacebar opens or closes the photo zoom mode, the right and left arrows go forward and backward, and the F2 function key focuses the field to rename a file (you can then exit with ESC, or save with Enter).
1111

12+
## Supported media types
13+
- Photos (e.g. jpg, gif, png)
14+
- Videos (e.g. mp4, webm, avi)
15+
- Audio recordings (e.g. mp3, wav)
16+
- Text documents (e.g. txt, log)
17+
- PDF documents
18+
1219
## Presentation
1320
You can watch the presentation here:
1421

wwwroot/js/site.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,24 @@ function loadVideo() {
8282
var video = document.getElementById("videoTagId");
8383
if (null != video) {
8484
video.load();
85-
video.focus({ focusVisible: true });
85+
}
86+
}
87+
function loadAudio() {
88+
var audio = document.getElementById("audioTagId");
89+
if (null != audio) {
90+
audio.load();
91+
}
92+
}
93+
function pauseOrPlayVideo() {
94+
var video = document.getElementById("videoTagId");
95+
if (null != video) {
96+
video.paused ? video.play() : video.pause();
97+
}
98+
}
99+
function pauseOrPlayAudio() {
100+
var audio = document.getElementById("audioTagId");
101+
if (null != audio) {
102+
audio.paused ? audio.play() : audio.pause();
86103
}
87104
}
88105
function unLoadVideo() {
@@ -162,6 +179,7 @@ async function configKeyBindModal() {
162179
window.addEventListener('keydown', function (event) {
163180
if (event.key == ' ' && document.activeElement.tagName != 'INPUT') {
164181
event.preventDefault();
182+
event.stopPropagation();
165183
}
166184
if (isListenForRegisterKey) {
167185
if (event.key == 'Shift') {
@@ -177,4 +195,10 @@ window.addEventListener('keydown', function (event) {
177195
else if (!document.activeElement.hasAttribute('data-prevent-keydown')) {
178196
dotNetObjRef.invokeMethodAsync('HandleKeyDown', event.key, isListenForRegisterKey, '');
179197
}
180-
});
198+
}, true);
199+
window.addEventListener('keyup', function (event) {
200+
if (event.key == ' ' && (document.activeElement.tagName == 'AUDIO' || document.activeElement.tagName == 'VIDEO')) {
201+
event.preventDefault();
202+
event.stopPropagation();
203+
}
204+
}, true);

0 commit comments

Comments
 (0)