mp3: Set finalizer for the stream

This commit is contained in:
Hajime Hoshi 2018-03-27 10:58:13 +09:00
parent f4e018a336
commit 88cfd219e7

View File

@ -21,6 +21,8 @@
package mp3 package mp3
import ( import (
"runtime"
"github.com/hajimehoshi/go-mp3" "github.com/hajimehoshi/go-mp3"
"github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/audio"
@ -51,6 +53,7 @@ func (s *Stream) Seek(offset int64, whence int) (int64, error) {
// Close is implementation of io.Closer's Close. // Close is implementation of io.Closer's Close.
func (s *Stream) Close() error { func (s *Stream) Close() error {
runtime.SetFinalizer(s, nil)
if s.resampling != nil { if s.resampling != nil {
return s.resampling.Close() return s.resampling.Close()
} }
@ -84,8 +87,10 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
if d.SampleRate() != context.SampleRate() { if d.SampleRate() != context.SampleRate() {
r = convert.NewResampling(d, d.Length(), d.SampleRate(), context.SampleRate()) r = convert.NewResampling(d, d.Length(), d.SampleRate(), context.SampleRate())
} }
return &Stream{ s := &Stream{
orig: d, orig: d,
resampling: r, resampling: r,
}, nil }
runtime.SetFinalizer(s, (*Stream).Close)
return s, nil
} }