audio: Bug fix: Implement renaderPlayer.IsPlayer correctly

This commit is contained in:
Hajime Hoshi 2021-01-16 01:01:19 +09:00
parent ad218eae2d
commit c742ae60bd
3 changed files with 13 additions and 11 deletions

View File

@ -321,7 +321,6 @@ func (p *Player) finalize() {
// //
// Close returns error when the player is already closed. // Close returns error when the player is already closed.
func (p *Player) Close() error { func (p *Player) Close() error {
runtime.SetFinalizer(p, nil)
return p.p.Close() return p.p.Close()
} }

View File

@ -107,6 +107,13 @@ func (p *Player) Play() {
p.cond.Signal() p.cond.Signal()
} }
func (p *Player) IsPlaying() bool {
p.cond.L.Lock()
defer p.cond.L.Unlock()
return p.state == playerStatePlaying
}
func (p *Player) Reset() { func (p *Player) Reset() {
p.cond.L.Lock() p.cond.L.Lock()
defer p.cond.L.Unlock() defer p.cond.L.Unlock()
@ -151,8 +158,10 @@ func (p *Player) close(remove bool) error {
return p.err return p.err
} }
p.v.Call("close", false) if p.v.Truthy() {
p.v = js.Undefined() p.v.Call("close", false)
p.v = js.Undefined()
}
if remove { if remove {
p.state = playerStateClosed p.state = playerStateClosed
p.onWritten.Release() p.onWritten.Release()

View File

@ -16,7 +16,6 @@ package audio
import ( import (
"io" "io"
"runtime"
"sync" "sync"
"time" "time"
) )
@ -30,6 +29,7 @@ type readerDriver interface {
type readerDriverPlayer interface { type readerDriverPlayer interface {
Pause() Pause()
Play() Play()
IsPlaying() bool
Reset() Reset()
Volume() float64 Volume() float64
SetVolume(volume float64) SetVolume(volume float64)
@ -51,7 +51,6 @@ type readerPlayer struct {
context *Context context *Context
player readerDriverPlayer player readerDriverPlayer
src *timeStream src *timeStream
playing bool
m sync.Mutex m sync.Mutex
} }
@ -66,7 +65,6 @@ func (c *readerPlayerFactory) newPlayerImpl(context *Context, src io.Reader) (pl
player: c.driver.NewPlayer(src), player: c.driver.NewPlayer(src),
src: s, src: s,
} }
runtime.SetFinalizer(p, (*readerPlayer).Close)
return p, nil return p, nil
} }
@ -75,7 +73,6 @@ func (p *readerPlayer) Play() {
defer p.m.Unlock() defer p.m.Unlock()
p.player.Play() p.player.Play()
p.playing = true
p.context.addPlayer(p) p.context.addPlayer(p)
} }
@ -84,14 +81,13 @@ func (p *readerPlayer) Pause() {
defer p.m.Unlock() defer p.m.Unlock()
p.player.Pause() p.player.Pause()
p.playing = false
} }
func (p *readerPlayer) IsPlaying() bool { func (p *readerPlayer) IsPlaying() bool {
p.m.Lock() p.m.Lock()
defer p.m.Unlock() defer p.m.Unlock()
return p.playing return p.player.IsPlaying()
} }
func (p *readerPlayer) Volume() float64 { func (p *readerPlayer) Volume() float64 {
@ -112,9 +108,7 @@ func (p *readerPlayer) Close() error {
p.m.Lock() p.m.Lock()
defer p.m.Unlock() defer p.m.Unlock()
runtime.SetFinalizer(p, nil)
p.context.removePlayer(p) p.context.removePlayer(p)
p.playing = false
return p.player.Close() return p.player.Close()
} }