audio/internal/readerdriver: Add Player.Err

This commit is contained in:
Hajime Hoshi 2021-03-28 21:57:39 +09:00
parent b46cb324ed
commit f4b770eff6
5 changed files with 30 additions and 4 deletions

View File

@ -149,7 +149,9 @@ func NewContext(sampleRate int) *Context {
return err
}
c.gcPlayers()
if err := c.gcPlayers(); err != nil {
return err
}
return nil
})
@ -206,7 +208,7 @@ func (c *Context) removePlayer(p playerImpl) {
c.m.Unlock()
}
func (c *Context) gcPlayers() {
func (c *Context) gcPlayers() error {
c.m.Lock()
defer c.m.Unlock()
@ -217,12 +219,17 @@ func (c *Context) gcPlayers() {
for p := range c.players {
rp, ok := p.(*readerPlayer)
if !ok {
return
return nil
}
if err := rp.Err(); err != nil {
return err
}
if !rp.IsPlaying() {
delete(c.players, p)
}
}
return nil
}
// IsReady returns a boolean value indicating whether the audio is ready or not.

View File

@ -200,6 +200,10 @@ func (p *Player) UnplayedBufferSize() int64 {
return int64(p.v.Get("unplayedBufferSize").Int())
}
func (p *Player) Err() error {
return p.err
}
func (p *Player) Close() error {
runtime.SetFinalizer(p, nil)
return p.close(true)

View File

@ -32,5 +32,6 @@ type Player interface {
Volume() float64
SetVolume(volume float64)
UnplayedBufferSize() int64
Err() error
io.Closer
}

View File

@ -259,12 +259,16 @@ func (p *playerImpl) UnplayedBufferSize() int64 {
return int64(sec * float64(p.context.sampleRate*p.context.channelNum*p.context.bitDepthInBytes))
}
func (p *playerImpl) Err() error {
return p.err
}
func (p *playerImpl) Close() error {
runtime.SetFinalizer(p, nil)
p.Reset()
p.state = readerPlayerClosed
p.appendBufferFunc.Release()
return p.err
return nil
}
type go2cppDriverWrapper struct {

View File

@ -199,6 +199,16 @@ func (p *readerPlayer) Seek(offset time.Duration) error {
return p.stream.Seek(offset)
}
func (p *readerPlayer) Err() error {
p.m.Lock()
defer p.m.Unlock()
if p.player == nil {
return nil
}
return p.player.Err()
}
func (p *readerPlayer) source() io.Reader {
return p.src
}