#Play Audios behind eachother
75 messages · Page 1 of 1 (latest)
Right now it just plays the last part
Thats because you tried to play while looping over it
they each get overridden by the next one
so only the last plays
I know, but how else can i do that
by queueing them
create a queue
How tho xD
There is no guide for a queue
store them in an object like an array and after they are done, pop them out
there isn't coz that's purely a js thing
But then i would be stuck with the same thing, a foreach loop
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
who said you have to use a loop?
i do not know how i could do that except for a loop
plays that song at index 0 > done? pop it out > repeat
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.
wait nvm.. I meant the other one
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.
- There are always a different amount of audios in that array
- I do not know the length of the audios, so i cannot wait
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.
this one.. finally!
haha third one is the charm xD
Yes, so you basicly add a listener that the song has ended
I forgot their names, my bad 😔
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
}
});
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
Ahh, smart, let me try
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);
});
Could this work (cant try cause im being rate-limited by some api 🥲 )
wait.. what is player?
that's not an array of your songs tho
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
you shift your array of songs, not the instance of the Player
just map url by their URL
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.
but my code would still work, wouldnt it?
but done
const songs = url.map(x => x.url);
should throw an error coz tht function doesn't exist on that class
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
ahhh, coz it doesn't have such an event
👍
Hey again, a small question i just cant seem to fix
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')
wdym sometimes? that's strange
Weired, a computer restart fixed that
Library: Audio Player
how??