audio: add (*Player).UnplayedBufferSize (#2021)

Closes #2020
This commit is contained in:
Hajime Hoshi 2022-03-22 16:55:56 +09:00 committed by GitHub
parent 8e6907c64a
commit 7744013b05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -399,6 +399,11 @@ func (p *Player) SetVolume(volume float64) {
p.p.SetVolume(volume)
}
// UnplayedBufferSize returns the amount of unplayed data in the player's buffer in the time duration.
func (p *Player) UnplayedBufferSize() time.Duration {
return p.p.UnplayedBufferSize()
}
type hook interface {
OnSuspendAudio(f func() error)
OnResumeAudio(f func() error)

View File

@ -245,8 +245,8 @@ func (p *playerImpl) Current() time.Duration {
return 0
}
sample := (p.stream.Current() - int64(p.player.UnplayedBufferSize())) / bytesPerSample
return time.Duration(sample) * time.Second / time.Duration(p.factory.sampleRate)
samples := (p.stream.Current() - int64(p.player.UnplayedBufferSize())) / bytesPerSample
return time.Duration(samples) * time.Second / time.Duration(p.factory.sampleRate)
}
func (p *playerImpl) Rewind() error {
@ -280,6 +280,18 @@ func (p *playerImpl) Err() error {
return p.player.Err()
}
func (p *playerImpl) UnplayedBufferSize() time.Duration {
p.m.Lock()
defer p.m.Unlock()
if p.player == nil {
return 0
}
samples := p.player.UnplayedBufferSize() / bytesPerSample
return time.Duration(samples) * time.Second / time.Duration(p.factory.sampleRate)
}
func (p *playerImpl) source() io.Reader {
return p.src
}