2

I have an audio stream. I'm sending that audio stream to Angular via API. Angular converts stream to URL and it plays. That works perfectly fine.Now I have a requirement of converting stream to URL from Node.js(v16.13.x) side itself. I have referred to some of the documentation, but it doesn't work.

How I convert Stream to URL (Angular)

byteArray = new Uint8Array(byteArray);
const blob = new Blob([byteArray], { type: 'audio/mpeg' });
const url = URL.createObjectURL(blob); //blob:http://localhost:4200/13cc5a20-4910-4d98-9e8c-c18751549dd2

What I tried on Nodejs

const { Blob } = require('node:buffer');
var byteArray = new Uint8Array(data.audioStream.data);
const blob = new Blob([byteArray], { type: 'audio/mpeg' });
const url = URL.createObjectURL(blob); //blob:nodedata:5c331f08-cc3b-4913-a054-1a5b7f65c625

I couldn't play blob:nodedata:5c331f08-cc3b-4913-a054-1a5b7f65c625 this. Am I missing something ? please help me out.

Felix Christo
  • 301
  • 1
  • 3
  • 19
  • Browser can play the URL which we get from `URL.createObjectURL(blob);` – Felix Christo Jul 13 '22 at 06:39
  • How did you try to "play" this? – Kaiido Jul 13 '22 at 06:47
  • Putting the URL on the Browser and the browser plays – Felix Christo Jul 13 '22 at 06:48
  • You won't be able to get the file unless the server allows that route. – qrsngky Jul 13 '22 at 06:48
  • My only issue is "I want to convert arrayBuffer to URL from Node.js" – Felix Christo Jul 13 '22 at 06:49
  • What do you think should happen if the objectURL is created by Node.js, and not by the browser itself, and the browser uses that? – qrsngky Jul 13 '22 at 06:51
  • @FelixChristo Like, should it try to request `blob:nodedata:5c331f08-cc3b-4913-a054-1a5b7f65c625`? But from which server? – qrsngky Jul 13 '22 at 06:53
  • After Node.js creates a URL, I will send that via API and Angular Plays that @qrsngky – Felix Christo Jul 13 '22 at 06:54
  • If your nodeJS runs express, for example, you can technically add a route that allows access to the object from there. – qrsngky Jul 13 '22 at 06:57
  • Yes my Node JS runs on Express – Felix Christo Jul 13 '22 at 06:58
  • @JaromandaX Indeed it seems to take quite a lot of unnecessary work (not sure if it's worth it, other than if you like experimenting), especially when you could just do something with express to stream that file (example: https://stackoverflow.com/questions/13106096/stream-files-in-node-express-to-client ) – qrsngky Jul 13 '22 at 07:03
  • I don't know whether I explained it correctly or not. I'm getting arrayBuffer from Amazon Lex to Node.JS. I'm forwarding that response to Angular and Angular converts the arrayBuffer to URL and it plays. Now I want that Angular's arrayBuffer to URL conversion to happen on Node.JS itself. – Felix Christo Jul 13 '22 at 07:07
  • Node.JS can do that part and pass the URL in API so Angular can play. Angular doesn't need that. Because I have Mobile client too. I don't want the conversion to happen in multiple places – Felix Christo Jul 13 '22 at 07:08
  • why do you need an objecturl for this though? an objecturl is not transferable – Jaromanda X Jul 13 '22 at 07:10
  • So, Can you tell me how to convert audio stream to URL from Node.JS side ? – Felix Christo Jul 13 '22 at 07:11
  • If you have the the arrayBuffer, can't you just use it to make a stream, skipping the objectURL step entirely? – qrsngky Jul 13 '22 at 07:13

0 Answers0