From 0315370577b313eb3702a444678ca1e50ee64725 Mon Sep 17 00:00:00 2001 From: omatztw <33661309+omatztw@users.noreply.github.com> Date: Wed, 23 Feb 2022 13:02:14 +0900 Subject: [PATCH] Stop goroutine when audio is done playing. Fix #17 --- dgvoice.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/dgvoice.go b/dgvoice.go index 073224b..3950dae 100644 --- a/dgvoice.go +++ b/dgvoice.go @@ -162,10 +162,17 @@ func PlayAudioFile(v *discordgo.VoiceConnection, filename string, stop <-chan bo // prevent memory leak from residual ffmpeg streams defer run.Process.Kill() + // Stop goroutine when audio is done playing + done := make(chan struct{}) + defer close(done) + //when stop is sent, kill ffmpeg go func() { - <-stop - err = run.Process.Kill() + select { + case <-stop: + err = run.Process.Kill() + case <-done: + } }() // Send "speaking" packet over the voice websocket