From 5f773a3277b7b32cea8d3314ecd8da4642a2773a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 16 May 2021 01:53:44 +0900 Subject: [PATCH] audio/internal/readerdriver: Refactoring --- audio/internal/oboe/binding_android.go | 10 ++++++++++ audio/internal/readerdriver/driver_android.go | 6 ++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/audio/internal/oboe/binding_android.go b/audio/internal/oboe/binding_android.go index 98e81168b..d6648a474 100644 --- a/audio/internal/oboe/binding_android.go +++ b/audio/internal/oboe/binding_android.go @@ -76,6 +76,9 @@ func (p *Player) AppendBuffer(buf []byte) { } func (p *Player) Play() error { + if p.player == 0 { + return fmt.Errorf("oboe: player is already closed at Play") + } if msg := C.Player_Play(p.player); msg != nil { return fmt.Errorf("oboe: Player_Play failed: %s", C.GoString(msg)) } @@ -83,6 +86,9 @@ func (p *Player) Play() error { } func (p *Player) Pause() error { + if p.player == 0 { + return fmt.Errorf("oboe: player is already closed at Pause") + } if msg := C.Player_Pause(p.player); msg != nil { return fmt.Errorf("oboe: Player_Pause failed: %s", C.GoString(msg)) } @@ -95,9 +101,13 @@ func (p *Player) SetVolume(volume float64) { func (p *Player) Close() error { runtime.SetFinalizer(p, nil) + if p.player == 0 { + return fmt.Errorf("oboe: player is already closed at Close") + } if msg := C.Player_Close(p.player); msg != nil { return fmt.Errorf("oboe: Player_Close failed: %s", C.GoString(msg)) } + p.player = 0 return nil } diff --git a/audio/internal/readerdriver/driver_android.go b/audio/internal/readerdriver/driver_android.go index 61a113186..459c22c1f 100644 --- a/audio/internal/readerdriver/driver_android.go +++ b/audio/internal/readerdriver/driver_android.go @@ -157,14 +157,12 @@ func (p *player) Reset() { if p.p == nil { return } - defer func() { - p.p = nil - p.cond.Signal() - }() if err := p.p.Close(); err != nil { p.setErrorImpl(err) return } + p.p = nil + p.cond.Signal() } func (p *player) Volume() float64 {