|
|
|
@ -1,6 +1,7 @@
@@ -1,6 +1,7 @@
|
|
|
|
|
import { IconComponent } from "@/shared"; |
|
|
|
|
import React from "react"; |
|
|
|
|
import React, { FC, useEffect } from "react"; |
|
|
|
|
import { useAudioPlayer, useAudioPosition } from "react-use-audio-player"; |
|
|
|
|
|
|
|
|
|
import playIcon from "@/assets/img/play-1-icon.svg"; |
|
|
|
|
import pauseIcon from "@/assets/img/pause-icon.svg"; |
|
|
|
|
import { AudioBar } from "./audio-bar.atom"; |
|
|
|
@ -19,38 +20,52 @@ export const PlayBar = () => {
@@ -19,38 +20,52 @@ export const PlayBar = () => {
|
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const AudioPlayer = ({ file, isMy }) => { |
|
|
|
|
const { togglePlayPause, ready, loading, playing } = useAudioPlayer({ |
|
|
|
|
src: file, |
|
|
|
|
format: "m4a", |
|
|
|
|
interface IAudioPlayerProps { |
|
|
|
|
fileUrl: string; |
|
|
|
|
fileId: number; |
|
|
|
|
isMy: boolean; |
|
|
|
|
activeAudioId: number; |
|
|
|
|
onPressPlay?: () => void; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const AudioPlayer: FC<IAudioPlayerProps> = ({ |
|
|
|
|
fileUrl, |
|
|
|
|
fileId, |
|
|
|
|
isMy, |
|
|
|
|
activeAudioId, |
|
|
|
|
onPressPlay, |
|
|
|
|
}) => { |
|
|
|
|
useEffect(() => { |
|
|
|
|
if (activeAudioId !== fileId && playing) player.pause(); |
|
|
|
|
}, [activeAudioId]); |
|
|
|
|
|
|
|
|
|
const { togglePlayPause, ready, loading, playing, player } = useAudioPlayer({ |
|
|
|
|
src: fileUrl, |
|
|
|
|
format: "mp4", |
|
|
|
|
autoplay: false, |
|
|
|
|
onpause: () => console.log("sound has paused!"), |
|
|
|
|
onplay: () => console.log("sound started play!"), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!ready && !loading) return <div>No audio to play</div>; |
|
|
|
|
if (loading) return <div>Loading audio</div>; |
|
|
|
|
|
|
|
|
|
const actionHandler = async () => { |
|
|
|
|
if (onPressPlay) onPressPlay(); |
|
|
|
|
await togglePlayPause(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<div className="player"> |
|
|
|
|
<div className="audio-controls"> |
|
|
|
|
{!playing ? ( |
|
|
|
|
<button |
|
|
|
|
className={ |
|
|
|
|
"player__button " + (isMy ? "my-sound-icon" : "sound-icon") |
|
|
|
|
} |
|
|
|
|
onClick={togglePlayPause} |
|
|
|
|
> |
|
|
|
|
<IconComponent name={playIcon} /> |
|
|
|
|
</button> |
|
|
|
|
) : ( |
|
|
|
|
<button |
|
|
|
|
className={ |
|
|
|
|
"player__button " + (isMy ? "my-sound-icon" : "sound-icon") |
|
|
|
|
} |
|
|
|
|
onClick={togglePlayPause} |
|
|
|
|
> |
|
|
|
|
<IconComponent name={pauseIcon} /> |
|
|
|
|
</button> |
|
|
|
|
)} |
|
|
|
|
<button |
|
|
|
|
className={ |
|
|
|
|
"player__button " + (isMy ? "my-sound-icon" : "sound-icon") |
|
|
|
|
} |
|
|
|
|
onClick={actionHandler} |
|
|
|
|
> |
|
|
|
|
<IconComponent name={playing ? pauseIcon : playIcon} /> |
|
|
|
|
</button> |
|
|
|
|
<PlayBar /> |
|
|
|
|
</div> |
|
|
|
|
</div> |
|
|
|
|