audio: Ensure Close is called at a readerPlayer is GCed

This commit is contained in:
Hajime Hoshi 2021-03-22 04:09:57 +09:00
parent 24973da123
commit 6f185063d6

View File

@ -17,6 +17,7 @@ package audio
import (
"fmt"
"io"
"runtime"
"sync"
"time"
)
@ -93,6 +94,7 @@ func (f *readerPlayerFactory) newPlayerImpl(context *Context, src io.Reader) (pl
stream: s,
factory: f,
}
runtime.SetFinalizer(p, (*readerPlayer).Close)
return p, nil
}
@ -177,8 +179,12 @@ func (p *readerPlayer) SetVolume(volume float64) {
func (p *readerPlayer) Close() error {
p.m.Lock()
defer p.m.Unlock()
runtime.SetFinalizer(p, nil)
if p.player != nil {
defer func() {
p.player = nil
}()
p.player.Pause()
return p.player.Close()
}