Feature - Prevents multiple audio/video attachments from being played at the same time (#24717)
This commit is contained in:
parent
389a6cc4c0
commit
d3cd37d73e
4 changed files with 140 additions and 11 deletions
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue