Closed SirFireball3521 closed 2 years ago
The Web Audio API exposes an EventEmitter with the ended
event:
audioElement.addEventListener("ended", () => {
// ...
}, false)
PR welcome for adding this.
This is running in terminal, not browser. Unless there's something obvious that I'm missing, I don't think there is an audio element
The Web Audio API is a best practice we should be following when doing something similar for Node.js.
So yeah, I'm missing something obvious. I'm somewhat new to this so if you don't mind me asking, what would the name of the audio element used by Audic be?
Audic is a class that you initialise with new Audic(path)
. The class needs to extend EventEmitter
and broadcast an event when it detects that the song has finished playing.
Alright, I'm really not sure how I go about that. Do I do something like this?
class Audic0 extends Audic extends EventEmitter
The class is created like this:
class Audic extends EventEmitter
You'll then need to get the duration and time which then needs to be used to determine when the audio has ended.
Ah okay I understand what you mean now. I apparently blanked on "PR welcome" and thought this was just for my code. Alright, when I have some time I might submit something.
Nevermind. I can tell what the stuff here is doing, but everything in vlc is beyond me and I don't have the capability to sort it out now. It's theoretically possible I come back to this in the future, but at the moment I'm not going to do anything.
Reopening so it can be tackled by someone else.
@SirFireball3521 Use this javascript file temporarily https://github.com/ajithgopi/audic/blob/master/source/index.js
GitHub
Play some audio. Contribute to ajithgopi/audic development by creating an account on GitHub.
I have found problems while setting the src
with the src
setter. When the src
is set and play()
is called immediately, the playback wont start because the async operation would be happening in background.
Maybe we can assign the async function we create in set src()
to this._setup
like this: Promise.all(this._setup, ...)
. This means that the setting task will always complete before it is possible to play the content.
It's still not possible to await the combined Promise in a setter function. The alternative solution would be to remove the setter and await the operations in the calling function.
But that would break existing codes if they update packages
It's still not possible to await the combined Promise in a setter function
We can await the Promise in the async closure that is created within the setter function
But that would break existing codes if they update packages
We can create a major release if we make a breaking change.
I have made some changes in my fork on commit 85aa0660ccb10652fa289447b0607847f102b060. I have also replaced the above syntax and made the callback a property which should now be used like
player.onComplete = function(){
console.log("Completed")
}
Settings the src should now be like:
async function playSong(){
await player.setSrc("F:\\Sample.wav")
player.play()
}
My objective was to play random other song when the current one ends... It's working perfectly right now.
I think it would be better if you pluck only the required codes from the commit and add manually. The old setter can be retained as is (it's not working anyways) so there wont be errors.
Describe the solution you'd like Add the ability to have a callback when a song ends. Something like:
or
This would be useful for something like a song queue.
Describe alternatives you've considered I could use
setTimeout()
with the duration of the file, but that won't account for pausing and resuming.