0

i'm sending a .wav file from server

app.use('/audio', upload.single('file'), async (req, res) => {
  let file = req.file;
  const destinationPath = await saveFile(req, res, file);
  const text = await stt(destinationPath);
  const reply = await talkToGPT(text);
  const replyAudioPath = await tts(reply);

  res.sendFile(replyAudioPath);
})

and i want to play this audio immediately on react client side, i don't know how to do this react please help me out

axios.post("http://localhost:8080/audio", 
  data, 
  config
)
.then((response) => {
    
})
.catch((error) => {
   console.log(error);
});
zxcvbnm
  • 19
  • 4
  • Hello, welcome to SO. Please read https://stackoverflow.com/help/how-to-ask – Hermanboxcar Jun 24 '23 at 10:13
  • Please make an attempt at this yourself by searching your question up online, on google, modzilla, etc. before coming here for help. We try to help people with problems in code, not write code for them. – Hermanboxcar Jun 24 '23 at 10:15
  • Does this answer your question? [Play audio with React](https://stackoverflow.com/questions/56602005/play-audio-with-react) – imjared Jun 24 '23 at 10:16
  • Why not just send the src URL of the file (in some JSON) and have the client code open that in an audio element? It would be more efficient than downloading the whole file and opening it. You could have a separate button that _does_ download it (without playing it). – Andy Jun 24 '23 at 10:31
  • update: .then((response) => { const blob = new Blob([response.data], { type: "audio/wav" }); const url = URL.createObjectURL(blob); const audio = new Audio(); audio.src = url; audio.play(); }) this is giving me error "Uncaught (in promise) DOMException: Failed to load because no supported source was found." – zxcvbnm Jun 24 '23 at 10:37
  • @Andy i'm building an assistant on top of chatgpt and i want the reponse/audio to be played without any interaction, that's why can't give a download option – zxcvbnm Jun 24 '23 at 10:42
  • Ok; re the first part of my comment: is there a reason you need to _download_ the file rather than have an audio element _link_ to a file on the server? @zxcvbnm – Andy Jun 24 '23 at 10:52
  • @Andy you're right, i should do the streaming instead – zxcvbnm Jun 24 '23 at 13:36

0 Answers0