diff --git a/audio/audio.go b/audio/audio.go index 6f682841c..cb3592495 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -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) diff --git a/audio/player.go b/audio/player.go index 75c4ac108..b7378055c 100644 --- a/audio/player.go +++ b/audio/player.go @@ -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 }