From dbd82645da3ce85388e0b613fde8397ea47b9e9e Mon Sep 17 00:00:00 2001 From: morethanwords Date: Wed, 26 Aug 2020 14:59:32 +0300 Subject: [PATCH] Fix audio & video simultaneous play --- src/components/appAudio.ts | 13 ++++++++++++- src/components/audio.ts | 11 ++++++++++- src/lib/appManagers/appDocsManager.ts | 2 -- src/lib/appManagers/appMediaViewer.ts | 5 +++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/components/appAudio.ts b/src/components/appAudio.ts index a0ecc01d..8f278f29 100644 --- a/src/components/appAudio.ts +++ b/src/components/appAudio.ts @@ -8,6 +8,8 @@ class AppAudio { private container: HTMLElement; private audios: {[mid: string]: HTMLAudioElement} = {}; private playingAudio: HTMLAudioElement; + + public willBePlayedAudio: HTMLAudioElement; private prevMid: number; private nextMid: number; @@ -109,7 +111,7 @@ class AppAudio { this.addAudio(message.media.document, mid); }); - console.log('loadSiblingsAudio', audio, type, mid, value, this.prevMid, this.nextMid); + //console.log('loadSiblingsAudio', audio, type, mid, value, this.prevMid, this.nextMid); }); } @@ -122,6 +124,15 @@ class AppAudio { this.playingAudio.pause(); } } + + public pause() { + if(!this.playingAudio || this.playingAudio.paused) return; + this.playingAudio.pause(); + } + + public willBePlayed(audio: HTMLAudioElement) { + this.willBePlayedAudio = audio; + } } const appAudio = new AppAudio(); diff --git a/src/components/audio.ts b/src/components/audio.ts index 8f7e0cc9..7e2b3b47 100644 --- a/src/components/audio.ts +++ b/src/components/audio.ts @@ -393,6 +393,8 @@ export default class AudioElement extends HTMLElement { const r = () => { onLoad(); + appAudio.willBePlayed(this.audio); // prepare for loading audio + if(!preloader) { preloader = new ProgressivePreloader(null, false); } @@ -405,7 +407,14 @@ export default class AudioElement extends HTMLElement { else this.addAudioListener('canplay', resolve); }).then(() => { downloadDiv.remove(); - this.audio.play(); + + //setTimeout(() => { + // release loaded audio + if(appAudio.willBePlayedAudio == this.audio) { + this.audio.play(); + appAudio.willBePlayedAudio = null; + } + //}, 10e3); }); }; diff --git a/src/lib/appManagers/appDocsManager.ts b/src/lib/appManagers/appDocsManager.ts index 53cb444b..9c3c1a26 100644 --- a/src/lib/appManagers/appDocsManager.ts +++ b/src/lib/appManagers/appDocsManager.ts @@ -105,8 +105,6 @@ class AppDocsManager { if(!apiDoc.mime_type) { switch(apiDoc.type) { case 'gif': - apiDoc.mime_type = 'video/mp4'; - break; case 'video': case 'round': apiDoc.mime_type = 'video/mp4'; diff --git a/src/lib/appManagers/appMediaViewer.ts b/src/lib/appManagers/appMediaViewer.ts index 6cc8ceeb..b702be41 100644 --- a/src/lib/appManagers/appMediaViewer.ts +++ b/src/lib/appManagers/appMediaViewer.ts @@ -12,6 +12,7 @@ import AvatarElement from "../../components/avatar"; import LazyLoadQueue from "../../components/lazyLoadQueue"; import appForward from "../../components/appForward"; import { isSafari, mediaSizes } from "../config"; +import appAudio from "../../components/appAudio"; export class AppMediaViewer { public wholeDiv = document.querySelector('.media-viewer-whole') as HTMLDivElement; @@ -795,6 +796,10 @@ export class AppMediaViewer { div.append(video); } + // fix for simultaneous play + appAudio.pause(); + appAudio.willBePlayedAudio = null; + const player = new VideoPlayer(video, true, media.supportsStreaming); return player; /* player.wrapper.parentElement.append(video);