Skip to content

Commit

Permalink
Controle de velocidade do áudio
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh authored Jun 10, 2022
1 parent 69d3927 commit baf20fb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
66 changes: 66 additions & 0 deletions frontend/src/components/Audio/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Button } from "@material-ui/core";
import React, { useRef } from "react";
import { useEffect } from "react";
import { useState } from "react";

const LS_NAME = 'audioMessageRate';

export default function({url}) {
const audioRef = useRef(null);
const [audioRate, setAudioRate] = useState( parseFloat(localStorage.getItem(LS_NAME) || "1") );
const [showButtonRate, setShowButtonRate] = useState(false);

useEffect(() => {
console.log('set-ar', audioRate);

audioRef.current.playbackRate = audioRate;
localStorage.setItem(LS_NAME, audioRate);
}, [audioRate]);

useEffect(() => {
audioRef.current.onplaying = () => {
setShowButtonRate(true);
};
audioRef.current.onpause = () => {
setShowButtonRate(false);
};
audioRef.current.onended = () => {
setShowButtonRate(false);
};
}, []);

const toogleRate = () => {
let newRate = null;

switch(audioRate) {
case 0.5:
newRate = 1;
break;
case 1:
newRate = 1.5;
break;
case 1.5:
newRate = 2;
break;
case 2:
newRate = 0.5;
break;
default:
newRate = 1;
break;
}

console.log('new-ar', newRate);

setAudioRate(newRate);
};

return (
<>
<audio ref={audioRef} controls>
<source src={url} type="audio/ogg"></source>
</audio>
{showButtonRate && <Button style={{marginLeft: "5px", marginTop: "-45px"}} onClick={toogleRate}>{audioRate}x</Button>}
</>
);
}
7 changes: 2 additions & 5 deletions frontend/src/components/MessagesList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import MessageOptionsMenu from "../MessageOptionsMenu";

import api from "../../services/api";
import toastError from "../../errors/toastError";
import Audio from "../Audio";

const useStyles = makeStyles((theme) => ({
messagesListWrapper: {
Expand Down Expand Up @@ -492,11 +493,7 @@ const MessagesList = ({ ticketId, isGroup }) => {
else if (message.mediaType === "image") {
return <ModalImageCors imageUrl={message.mediaUrl} />;
} else if (message.mediaType === "audio") {
return (
<audio controls>
<source src={message.mediaUrl} type="audio/ogg"></source>
</audio>
);
return <Audio url={message.mediaUrl} />
} else if (message.mediaType === "video") {
return (
<video
Expand Down

0 comments on commit baf20fb

Please sign in to comment.