#Play Audios behind eachother

75 messages · Page 1 of 1 (latest)

carmine nexus
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

surreal zinc
#

Right now it just plays the last part

honest quail
#

Thats because you tried to play while looping over it

#

they each get overridden by the next one

#

so only the last plays

surreal zinc
#

I know, but how else can i do that

loud creek
#

by queueing them

honest quail
#

create a queue

surreal zinc
#

How tho xD

modern finchBOT
surreal zinc
loud creek
loud creek
surreal zinc
loud creek
#

waitWhat how??

#

please enlighten us with an example

surreal zinc
#

if i have an array

let array = ["url1", "url2", "url3"]

#

And how can i play them behind each other? I would probably use an foreach loop to play it

#

but thats excatly the same code as i have now

loud creek
#

who said you have to use a loop?

surreal zinc
#

i do not know how i could do that except for a loop

loud creek
#

plays that song at index 0 > done? pop it out > repeat

modern finchBOT
#

mdn Array.prototype.pop()
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

loud creek
#

wait nvm.. I meant the other one

modern finchBOT
#

mdn Array.prototype.unshift()
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

surreal zinc
#
  1. There are always a different amount of audios in that array
  2. I do not know the length of the audios, so i cannot wait
modern finchBOT
#

mdn Array.prototype.shift()
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

honest quail
#

haha third one is the charm xD

#

Yes, so you basicly add a listener that the song has ended

loud creek
#

I forgot their names, my bad 😔

honest quail
#

then you do another shift on the array etc..

#

until the array is empty

surreal zinc
#

Could you maybe provide me an code-example? Im a bit confused

#

i know how to do it with one audio, but not with multiple

#

Already tried something similar tho

#

  url.forEach(async element => {
    let done = false
    const resource = createAudioResource(element.url);
    let play = player.play(resource);
    player.on('end', done = true);
    connection.subscribe(player);
    while (true){
      if(done == true)return
    }
  });
loud creek
#
const songs = ['Song1', 'Song2', ...];

// always attach 'songs[0]' to the audio resource
// listen for 'end' event on [player] then pop the first index out with <Array>.shift()
// is there another song in the array? if yes, then repeat 1st step otherwise <Action>
// <Action> can be like leaving Voice Channel
surreal zinc
#

Ahh, smart, let me try

surreal zinc
loud creek
#

wait.. what is player?

surreal zinc
#

const player = createAudioPlayer();

#

assigned on top of the function

loud creek
#

that's not an array of your songs tho

surreal zinc
#

I just tested it: Doesnt work, it plays the first one and then stops

#
function play(connection, message){

  let songs = []

  const player = createAudioPlayer();

  const url = googleTTS.getAllAudioUrls(message, {
    lang: langkurz,
    slow: false,
    host: 'https://translate.google.com',
    timeout: 10000,

  });

  url.forEach(async element => {
    songs.push(element.url)
  });


  const resource = createAudioResource(songs[0]);
  connection.subscribe(player);
  player.play(resource);

  player.on("end", () => {
    console.log('The audio player has stopped playing!');
   player.shift()
   const resource = createAudioResource(songs[0]);
   connection.subscribe(player);
   player.play(resource);
});
  

  
  
}
#

Full function

loud creek
#

you shift your array of songs, not the instance of the Player

#

just map url by their URL

surreal zinc
#

uhh

#

im confused

modern finchBOT
#

mdn Array.prototype.map()
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

surreal zinc
#

but done

#

const songs = url.map(x => x.url);

loud creek
surreal zinc
#

Ok, ill try now

#

Still not working

loud creek
#

wdym by not working?

surreal zinc
#

player.on("end", () => {
console.log('The audio player has stopped playing!');
player.shift()
const resource = createAudioResource(songs[0]);
connection.subscribe(player);
player.play(resource);
});

#

The "The audio player has stopped playing" doesnt even get logged

loud creek
#

ahhh, coz it doesn't have such an event

surreal zinc
#

player.on(AudioPlayerStatus.Idle, () => { fixed it

#

works now, ty!

loud creek
#

👍

surreal zinc
#
  const resource = createAudioResource(songs[0], {
        inlineVolume: true
  })
  const player = createAudioPlayer()
  connection.subscribe(player);
  player.play(resource);

Sometimes it says
TypeError: Cannot read properties of undefined (reading 'subscribe')

loud creek
#

wdym sometimes? that's strange

surreal zinc
#

i know

#

thats why im asking

#

it def. is connected

loud creek
#

mind showing a snippet of the full code?

#

should include where you defined connection

surreal zinc