change the class' name and add the method pauseCurrentAudio
This commit is contained in:
parent
0738f60f8e
commit
8cab77f94e
|
@ -15,7 +15,7 @@ import styles from './styles';
|
||||||
import Slider from './Slider';
|
import Slider from './Slider';
|
||||||
import AudioRate from './AudioRate';
|
import AudioRate from './AudioRate';
|
||||||
import PlayButton from './PlayButton';
|
import PlayButton from './PlayButton';
|
||||||
import handleAudioMedia from '../../../../lib/methods/handleAudioMedia';
|
import audioPlayer from '../../../../lib/methods/audioPlayer';
|
||||||
|
|
||||||
interface IMessageAudioProps {
|
interface IMessageAudioProps {
|
||||||
file: IAttachment;
|
file: IAttachment;
|
||||||
|
@ -43,16 +43,15 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style, msg }: IMe
|
||||||
const onPlaybackStatusUpdate = (status: AVPlaybackStatus) => {
|
const onPlaybackStatusUpdate = (status: AVPlaybackStatus) => {
|
||||||
if (status) {
|
if (status) {
|
||||||
onPlaying(status);
|
onPlaying(status);
|
||||||
onLoad(status);
|
handlePlaybackStatusUpdate(status);
|
||||||
onProgress(status);
|
|
||||||
onEnd(status);
|
onEnd(status);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const loadAudio = async (audio: string) => {
|
const loadAudio = async (audio: string) => {
|
||||||
await handleAudioMedia.loadAudio(audio);
|
await audioPlayer.loadAudio(audio);
|
||||||
audioUri.current = audio;
|
audioUri.current = audio;
|
||||||
handleAudioMedia.setOnPlaybackStatusUpdate(audio, onPlaybackStatusUpdate);
|
audioPlayer.setOnPlaybackStatusUpdate(audio, onPlaybackStatusUpdate);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onPlaying = (data: AVPlaybackStatus) => {
|
const onPlaying = (data: AVPlaybackStatus) => {
|
||||||
|
@ -63,20 +62,15 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style, msg }: IMe
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const onLoad = (data: AVPlaybackStatus) => {
|
const handlePlaybackStatusUpdate = (data: AVPlaybackStatus) => {
|
||||||
if (data.isLoaded && data.durationMillis) {
|
if (data.isLoaded && data.durationMillis) {
|
||||||
const durationSeconds = data.durationMillis / 1000;
|
const durationSeconds = data.durationMillis / 1000;
|
||||||
duration.value = durationSeconds > 0 ? durationSeconds : 0;
|
duration.value = durationSeconds > 0 ? durationSeconds : 0;
|
||||||
setRate(data.rate);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const onProgress = (data: AVPlaybackStatus) => {
|
|
||||||
if (data.isLoaded) {
|
|
||||||
const currentSecond = data.positionMillis / 1000;
|
const currentSecond = data.positionMillis / 1000;
|
||||||
if (currentSecond <= duration.value) {
|
if (currentSecond <= durationSeconds) {
|
||||||
currentTime.value = currentSecond;
|
currentTime.value = currentSecond;
|
||||||
}
|
}
|
||||||
|
setRate(data.rate);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -94,7 +88,7 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style, msg }: IMe
|
||||||
};
|
};
|
||||||
|
|
||||||
const setPosition = async (time: number) => {
|
const setPosition = async (time: number) => {
|
||||||
await handleAudioMedia.setPositionAsync(audioUri.current, time);
|
await audioPlayer.setPositionAsync(audioUri.current, time);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getUrl = () => {
|
const getUrl = () => {
|
||||||
|
@ -108,9 +102,9 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style, msg }: IMe
|
||||||
const togglePlayPause = async () => {
|
const togglePlayPause = async () => {
|
||||||
try {
|
try {
|
||||||
if (!paused) {
|
if (!paused) {
|
||||||
await handleAudioMedia.pauseAudio(audioUri.current);
|
await audioPlayer.pauseAudio(audioUri.current);
|
||||||
} else {
|
} else {
|
||||||
await handleAudioMedia.playAudio(audioUri.current);
|
await audioPlayer.playAudio(audioUri.current);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
|
@ -118,7 +112,7 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style, msg }: IMe
|
||||||
};
|
};
|
||||||
|
|
||||||
const onChangeRate = async (value = 1.0) => {
|
const onChangeRate = async (value = 1.0) => {
|
||||||
await handleAudioMedia.setRateAsync(audioUri.current, value);
|
await audioPlayer.setRateAsync(audioUri.current, value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDownload = async () => {
|
const handleDownload = async () => {
|
||||||
|
|
|
@ -11,7 +11,7 @@ const mode = {
|
||||||
interruptionModeAndroid: InterruptionModeAndroid.DoNotMix
|
interruptionModeAndroid: InterruptionModeAndroid.DoNotMix
|
||||||
};
|
};
|
||||||
|
|
||||||
class HandleAudioMedia {
|
class AudioPlayer {
|
||||||
private audioQueue: { [uri: string]: Sound };
|
private audioQueue: { [uri: string]: Sound };
|
||||||
private audioPlaying: string;
|
private audioPlaying: string;
|
||||||
|
|
||||||
|
@ -67,6 +67,12 @@ class HandleAudioMedia {
|
||||||
this.audioPlaying = '';
|
this.audioPlaying = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async pauseCurrentAudio() {
|
||||||
|
if (this.audioPlaying) {
|
||||||
|
await this.pauseAudio(this.audioPlaying);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async setPositionAsync(uri: string, time: number) {
|
async setPositionAsync(uri: string, time: number) {
|
||||||
try {
|
try {
|
||||||
await this.audioQueue[uri]?.setPositionAsync(time);
|
await this.audioQueue[uri]?.setPositionAsync(time);
|
||||||
|
@ -86,6 +92,12 @@ class HandleAudioMedia {
|
||||||
this.audioPlaying = '';
|
this.audioPlaying = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async unloadCurrentAudio() {
|
||||||
|
if (this.audioPlaying) {
|
||||||
|
await this.unloadAudio(this.audioPlaying);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async unloadAllAudios() {
|
async unloadAllAudios() {
|
||||||
const audiosLoaded = Object.values(this.audioQueue);
|
const audiosLoaded = Object.values(this.audioQueue);
|
||||||
try {
|
try {
|
||||||
|
@ -103,5 +115,5 @@ class HandleAudioMedia {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleAudioMedia = new HandleAudioMedia();
|
const audioPlayer = new AudioPlayer();
|
||||||
export default handleAudioMedia;
|
export default audioPlayer;
|
|
@ -37,5 +37,5 @@ export * from './crashReport';
|
||||||
export * from './parseSettings';
|
export * from './parseSettings';
|
||||||
export * from './subscribeRooms';
|
export * from './subscribeRooms';
|
||||||
export * from './serializeAsciiUrl';
|
export * from './serializeAsciiUrl';
|
||||||
export * from './handleAudioMedia';
|
export * from './audioPlayer';
|
||||||
export * from './isRoomFederated';
|
export * from './isRoomFederated';
|
||||||
|
|
Loading…
Reference in New Issue