1

I am trying to play audio files but the thing is the source of audio is coming from socket. When multiple audio's URL come from socket one after another in some fraction of seconds then latest URL is playing but I want to play the audio file one after another

Here below is the code

useEffect(() => {
    socket.on("newsAudioStream", async (AudioStream) => {
      if (speech && settings.rFeeds) {
        const uInt8Array = new Uint8Array(AudioStream);
        const arrayBuffer = uInt8Array.buffer;
        const blob = new Blob([arrayBuffer]);
        const url = URL.createObjectURL(blob);

        pollyPlayerRef.current.src = url;
        pollyPlayerRef.current.load();
        pollyPlayerRef.current.play();
      }
    });
    return () => socket.off("newsAudioStream");
}, [settings.rFeeds, speech]);

Can anyone please help me to fix this problem.

Abhijit
  • 123
  • 2
  • 15
  • 1
    Maybe push the AudioStreams in a queue and then write a queue consumer that decides when to play a AudioStream(or skip it in case you want to). Queue would give you full control over these things. To know if a currently playing video has ended you can follow https://stackoverflow.com/a/2880950/1109657 Consume next item from queue after the `ended` event has fired. – Hariom Balhara Aug 27 '21 at 09:06
  • Can you please provide any code example for queue? – Abhijit Aug 27 '21 at 09:14
  • 1
    Queue can be an array that you are looping over(maintaining the currently iterated item index in some other variable). Increment the index when current video ends. – Hariom Balhara Aug 27 '21 at 09:21
  • Thank you so much bro. Can you please upvote this question so that others can get benefits from it. – Abhijit Aug 27 '21 at 09:46

0 Answers0