faiface / beep

A little package that brings sound to any Go application. Suitable for playback and audio-processing.
MIT License
2.08k stars 152 forks source link

Is it possible to know when a speaker.Play() has finished? #17

Closed zapic0 closed 6 years ago

zapic0 commented 6 years ago

If I call speaker.Play() inside a loop, for example, it is just played for a little fraction of time (if I set a Sleep the sound is playing for as long as the sleep). Is there any way to know a file duration?

faiface commented 6 years ago

Yes, it is possible and you don't even have to sleep for the right amount of time (which would be imprecise anyway). Just for the info, you can get the duration of an audio file by taking its streamer and calling the Len method. That returns you back the number of samples, which you can convert to time.Duration using the format.D method.

A better way to achieve what you want is to use beep.Callback. The method is nicely described in the README, so check it out there.

zapic0 commented 6 years ago

Thanks @faiface, my fault I didn't see the Callback doc, sorry and thanks for the library!