[FIX] Playing multiple voice messages at the same time (#4662)

Co-authored-by: gitstart <gitstart@users.noreply.github.com>
Co-authored-by: Debojyoti Singha <20729878+debojyoti452@users.noreply.github.com>
Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
GitStart 2022-11-16 14:40:45 +01:00 committed by Diego Mello
parent 9218cec8bf
commit 0ca4d4b30f
2 changed files with 13 additions and 0 deletions

View File

@ -20,6 +20,8 @@ import { TGetCustomEmoji } from '../../definitions/IEmoji';
import { IAttachment } from '../../definitions'; import { IAttachment } from '../../definitions';
import { TSupportedThemes } from '../../theme'; import { TSupportedThemes } from '../../theme';
import { downloadAudioFile } from '../../lib/methods/audioFile'; import { downloadAudioFile } from '../../lib/methods/audioFile';
import EventEmitter from '../../lib/methods/helpers/events';
import { PAUSE_AUDIO } from './constants';
interface IButton { interface IButton {
loading: boolean; loading: boolean;
@ -128,6 +130,11 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
this.sound.setOnPlaybackStatusUpdate(this.onPlaybackStatusUpdate); this.sound.setOnPlaybackStatusUpdate(this.onPlaybackStatusUpdate);
} }
pauseSound = () => {
EventEmitter.removeListener(PAUSE_AUDIO, this.pauseSound);
this.togglePlayPause();
};
async componentDidMount() { async componentDidMount() {
const { file, messageId } = this.props; const { file, messageId } = this.props;
const { baseUrl, user } = this.context; const { baseUrl, user } = this.context;
@ -183,6 +190,7 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
} }
async componentWillUnmount() { async componentWillUnmount() {
EventEmitter.removeListener(PAUSE_AUDIO, this.pauseSound);
try { try {
await this.sound.stopAsync(); await this.sound.stopAsync();
} catch { } catch {
@ -221,6 +229,7 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
try { try {
await this.sound.stopAsync(); await this.sound.stopAsync();
this.setState({ paused: true, currentTime: 0 }); this.setState({ paused: true, currentTime: 0 });
EventEmitter.removeListener(PAUSE_AUDIO, this.pauseSound);
} catch { } catch {
// do nothing // do nothing
} }
@ -243,7 +252,10 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
try { try {
if (paused) { if (paused) {
await this.sound.pauseAsync(); await this.sound.pauseAsync();
EventEmitter.removeListener(PAUSE_AUDIO, this.pauseSound);
} else { } else {
EventEmitter.emit(PAUSE_AUDIO);
EventEmitter.addEventListener(PAUSE_AUDIO, this.pauseSound);
await Audio.setAudioModeAsync(mode); await Audio.setAudioModeAsync(mode);
await this.sound.playAsync(); await this.sound.playAsync();
} }

View File

@ -1,2 +1,3 @@
export const DISCUSSION = 'discussion'; export const DISCUSSION = 'discussion';
export const THREAD = 'thread'; export const THREAD = 'thread';
export const PAUSE_AUDIO = 'pause_audio';