Skip to content

Commit

Permalink
Add audio file support
Browse files Browse the repository at this point in the history
  • Loading branch information
floogulinc committed Sep 25, 2021
1 parent f86a2e1 commit e512508
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app/hydrus-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface HydrusFileList {
export enum HydrusFileType {
Image,
Video,
Audio,
Flash,
Unsupported
}
8 changes: 8 additions & 0 deletions src/app/hydrus-files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ export class HydrusFilesService {
].includes(mime)) {
return HydrusFileType.Video;
}
if ([
'audio/mp3',
'audio/ogg',
'audio/flac',
'audio/x-wav',
].includes(mime)) {
return HydrusFileType.Audio;
}
if ([
'video/x-flv',
'application/x-shockwave-flash'
Expand Down
9 changes: 9 additions & 0 deletions src/app/photoswipe/photoswipe.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ $pswp__include-minimal-style: true;
height: 100%;
}


.pswp-video {
width: 100%;
height: 100%;
Expand All @@ -27,6 +28,14 @@ $pswp__include-minimal-style: true;
}
}

.pswp-audio-container {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
}

.pswp-video-placeholder {
width: 100%;
height: 100%;
Expand Down
29 changes: 29 additions & 0 deletions src/app/photoswipe/photoswipe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ export class PhotoswipeService {
`,
pid: file.file_id
};
} else if (file.file_type === HydrusFileType.Audio) {
return {
html: `
<div id="pswp-audio-${file.file_id}" class="pswp-audio-container">
</div>
`,
pid: file.file_id
};
} else {
const html = file.has_thumbnail
? `<div class="pswp__error-msg">
Expand Down Expand Up @@ -104,19 +112,31 @@ export class PhotoswipeService {
});
};

const removeAudio = () => {
ps.container.querySelectorAll<HTMLVideoElement>('.pswp-audio').forEach((audio) => {
audio.pause();
audio.removeAttribute('src');
audio.load();
audio.remove();
});
};

ps.listen('close', () => {
this.psClose$.next();
});
ps.listen('destroy', () => {
removeVideos();
removeAudio();
});
ps.listen('beforeChange', () => {
removeVideos();
removeAudio();
});
ps.listen('afterChange', () => {
if (ps.currItem.html) {
const pid = (ps.currItem as PhotoSwipe.Item).pid;
const vidContainer = ps.container.querySelector<HTMLDivElement>(`#pswp-video-${pid}`);
const audioContainer = ps.container.querySelector<HTMLDivElement>(`#pswp-audio-${pid}`);
if (vidContainer) {
const item = items.find(i => i.file_id === pid);
const vid = document.createElement('video');
Expand All @@ -127,6 +147,15 @@ export class PhotoswipeService {
vid.loop = true;
vid.className = 'pswp-video';
vidContainer.prepend(vid);
} else if (audioContainer) {
const item = items.find(i => i.file_id === pid);
const audio = document.createElement('audio');
audio.src = item.file_url;
audio.autoplay = true;
audio.loop = true;
audio.controls = true;
audio.className = 'pswp-audio';
audioContainer.prepend(audio);
}
}
});
Expand Down

0 comments on commit e512508

Please sign in to comment.