Feature - Prevents multiple audio/video attachments from being played at the same time (#24717)

This commit is contained in:
João Pedro Marques authored and GitHub committed 2023-11-07 23:37:58 +00:00
1 parent 389a6cc4c0
commit d3cd37d73e
4 files changed
+140 -11

No files matched your search

@@ -20,6 +20,7 @@ import { formatTime, getPointerPosition, fileNameFromURL } from 'mastodon/featur
import { Blurhash } from '../../components/blurhash';
import { displayMedia, useBlurhash } from '../../initial_state';
import { currentMedia, setCurrentMedia } from '../../reducers/media_attachments';
import Visualizer from './visualizer';
@@ -165,15 +166,32 @@ class Audio extends PureComponent {
}
togglePlay = () => {
if (!this.audioContext) {
this._initAudioContext();
const audios = document.querySelectorAll('audio');
audios.forEach((audio) => {
const button = audio.previousElementSibling;
button.addEventListener('click', () => {
if(audio.paused) {
audios.forEach((e) => {
if (e !== audio) {
e.pause();
}
});
audio.play();
this.setState({ paused: false });
} else {
audio.pause();
this.setState({ paused: true });
}
});
});
if (currentMedia !== null) {
currentMedia.pause();
}
if (this.state.paused) {
this.setState({ paused: false }, () => this.audio.play());
} else {
this.setState({ paused: true }, () => this.audio.pause());
}
this.audio.play();
setCurrentMedia(this.audio);
};
handleResize = debounce(() => {
@@ -195,6 +213,7 @@ class Audio extends PureComponent {
};
handlePause = () => {
this.audio.pause();
this.setState({ paused: true });
if (this.audioContext) {