Skip to content

Commit

Permalink
audio: Ensure Close is called at a readerPlayer is GCed
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Mar 21, 2021
1 parent 24973da commit 6f18506
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions audio/readerplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package audio
import (
"fmt"
"io"
"runtime"
"sync"
"time"
)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 6f18506

Please sign in to comment.