Skip to content

Commit

Permalink
Ajuste para audio no IOS
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Aug 30, 2023
1 parent cf93086 commit 163d08c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 47 deletions.
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@types/express": "^4.17.13",
"@types/factory-girl": "^5.0.2",
"@types/faker": "^5.1.3",
"@types/fluent-ffmpeg": "^2.1.21",
"@types/jest": "^26.0.15",
"@types/jsonwebtoken": "^8.5.0",
"@types/lodash": "^4.14.185",
Expand Down
42 changes: 39 additions & 3 deletions backend/src/services/WbotServices/wbotMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Client
} from "whatsapp-web.js";

import * as ffmpeg from "fluent-ffmpeg";
import Contact from "../../models/Contact";
import Ticket from "../../models/Ticket";
import Message from "../../models/Message";
Expand All @@ -30,6 +31,8 @@ import UpdateTicketService from "../TicketServices/UpdateTicketService";
import CreateContactService from "../ContactServices/CreateContactService";
import formatBody from "../../helpers/Mustache";

ffmpeg.setFfmpegPath("/usr/bin/ffmpeg");

const request = require("request");

interface Session extends Client {
Expand Down Expand Up @@ -141,12 +144,45 @@ const verifyMediaMessage = async (
media.filename = `${new Date().getTime()}${originalFilename}`;
}

// try {
// await writeFileAsync(
// join(__dirname, "..", "..", "..", "public", media.filename),
// media.data,
// "base64"
// );
// } catch (err: any) {
// Sentry.captureException(err);
// logger.error(err);
// }

try {
await writeFileAsync(
join(__dirname, "..", "..", "..", "public", media.filename),
media.data,
"base64"
);
)
.then(() => {
console.log("Arquivo OGG salvo com sucesso!");
return new Promise<void>((resolve, reject) => {
ffmpeg(`./public/${media.filename}`)
.toFormat("mp3")
.save(`./public/${media.filename}`.replace(".ogg", ".mp3"))
.on("end", () => {
resolve();
})
.on("error", err => {
reject(err);
});
});
})
.then(() => {
console.log("Conversão concluída!");
// Aqui você pode fazer o que desejar com o arquivo MP3 convertido.
})
.catch(err => {
console.error("Ocorreu um erro:", err);
// Trate o erro de acordo com sua lógica de aplicativo.
});
} catch (err: any) {
Sentry.captureException(err);
logger.error(err);
Expand All @@ -156,15 +192,15 @@ const verifyMediaMessage = async (
id: msg.id.id,
ticketId: ticket.id,
contactId: msg.fromMe ? undefined : contact.id,
body: msg.body || media.filename,
body: msg.body,
fromMe: msg.fromMe,
read: msg.fromMe,
mediaUrl: media.filename,
mediaType: media.mimetype.split("/")[0],
quotedMsgId: quotedMsg?.id
};

await ticket.update({ lastMessage: msg.body || media.filename });
await ticket.update({ lastMessage: msg.body });
const newMessage = await CreateMessageService({ messageData });

return newMessage;
Expand Down
2 changes: 1 addition & 1 deletion docs/INSTALL_VPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ sudo apt-get install -y nodejs
20. Instalação de libs

```bash
sudo apt install apt-transport-https ca-certificates curl software-properties-common git
sudo apt install apt-transport-https ca-certificates curl software-properties-common git ffmpeg
```

21. Atualizando
Expand Down
107 changes: 64 additions & 43 deletions frontend/src/components/Audio/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,77 @@ const LS_NAME = 'audioMessageRate';

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

const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;

useEffect(() => {
audioRef.current.playbackRate = audioRate;
localStorage.setItem(LS_NAME, 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);
};
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;
}

setAudioRate(newRate);
const toggleRate = () => {
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;
}
setAudioRate(newRate);
};


const getAudioSource = () => {
let sourceUrl = url;

if (isIOS) {
sourceUrl = sourceUrl.replace(".ogg", ".mp3");
}

return (
<source src={sourceUrl} type={isIOS ? "audio/mp3" : "audio/ogg"} />
);
};

return (
<>
<audio ref={audioRef} controls>
<source src={url} type="audio/mp3"></source>
</audio>
{showButtonRate && <Button style={{marginLeft: "5px", marginTop: "-45px"}} onClick={toogleRate}>{audioRate}x</Button>}
</>
<>
<audio ref={audioRef} controls>
{getAudioSource()}
</audio>
{showButtonRate && (
<Button
style={{ marginLeft: "5px", marginTop: "-45px" }}
onClick={toggleRate}
>
{audioRate}x
</Button>
)}
</>
);
}

Expand Down

0 comments on commit 163d08c

Please sign in to comment.