update Oto for a player's Seek

Updates hajimehoshi/oto#172
This commit is contained in:
Hajime Hoshi 2022-07-01 02:21:17 +09:00
parent abc8697ea3
commit 12d274eb0a
4 changed files with 57 additions and 21 deletions

View File

@ -21,6 +21,7 @@
package cbackend package cbackend
import ( import (
"errors"
"io" "io"
"runtime" "runtime"
"sync" "sync"
@ -250,6 +251,34 @@ func (p *playerImpl) Pause() {
p.state = playerPaused p.state = playerPaused
} }
func (p *Player) Seek(offset int64, whence int) (int64, error) {
return p.p.Seek(offset, whence)
}
func (p *playerImpl) Seek(offset int64, whence int) (int64, error) {
p.m.Lock()
defer p.m.Unlock()
if p.state == playerPlay {
defer p.playImpl()
}
// Reset the internal buffer.
p.resetImpl()
// Check if the source implements io.Seeker.
s, ok := p.src.(io.Seeker)
if !ok {
return 0, errors.New("cbackend: the source must implement io.Seeker")
}
newOffset, err := s.Seek(offset, whence)
if err != nil {
return newOffset, err
}
return newOffset, nil
}
func (p *Player) Reset() { func (p *Player) Reset() {
p.p.Reset() p.p.Reset()
} }

View File

@ -33,6 +33,7 @@ type player interface {
UnplayedBufferSize() int UnplayedBufferSize() int
Err() error Err() error
SetBufferSize(bufferSize int) SetBufferSize(bufferSize int)
io.Seeker
io.Closer io.Closer
} }
@ -267,13 +268,11 @@ func (p *playerImpl) Seek(offset time.Duration) error {
return err return err
} }
if p.player.IsPlaying() { pos := p.stream.timeDurationToPos(offset)
defer func() { if _, err := p.player.Seek(pos, io.SeekStart); err != nil {
p.player.Play() return err
}()
} }
p.player.Reset() return nil
return p.stream.Seek(offset)
} }
func (p *playerImpl) Err() error { func (p *playerImpl) Err() error {
@ -338,7 +337,25 @@ func (s *timeStream) Read(buf []byte) (int, error) {
return n, err return n, err
} }
func (s *timeStream) Seek(offset time.Duration) error { func (s *timeStream) Seek(offset int64, whence int) (int64, error) {
s.m.Lock()
defer s.m.Unlock()
seeker, ok := s.r.(io.Seeker)
if !ok {
// TODO: Should this return an error?
panic("audio: the source must be io.Seeker when seeking but not")
}
pos, err := seeker.Seek(offset, whence)
if err != nil {
return pos, err
}
s.pos = pos
return pos, nil
}
func (s *timeStream) timeDurationToPos(offset time.Duration) int64 {
s.m.Lock() s.m.Lock()
defer s.m.Unlock() defer s.m.Unlock()
@ -348,17 +365,7 @@ func (s *timeStream) Seek(offset time.Duration) error {
o -= o % bytesPerSample o -= o % bytesPerSample
o += s.pos % bytesPerSample o += s.pos % bytesPerSample
seeker, ok := s.r.(io.Seeker) return o
if !ok {
panic("audio: the source must be io.Seeker when seeking but not")
}
pos, err := seeker.Seek(o, io.SeekStart)
if err != nil {
return err
}
s.pos = pos
return nil
} }
func (s *timeStream) Current() int64 { func (s *timeStream) Current() int64 {

2
go.mod
View File

@ -8,7 +8,7 @@ require (
github.com/hajimehoshi/bitmapfont/v2 v2.2.0 github.com/hajimehoshi/bitmapfont/v2 v2.2.0
github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41 github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41
github.com/hajimehoshi/go-mp3 v0.3.3 github.com/hajimehoshi/go-mp3 v0.3.3
github.com/hajimehoshi/oto/v2 v2.2.0-alpha.3.0.20220627134823-65dbd988610c github.com/hajimehoshi/oto/v2 v2.2.0-alpha.3.0.20220630171212-19c3101d3014
github.com/jakecoffman/cp v1.2.0 github.com/jakecoffman/cp v1.2.0
github.com/jezek/xgb v1.0.1 github.com/jezek/xgb v1.0.1
github.com/jfreymuth/oggvorbis v1.0.3 github.com/jfreymuth/oggvorbis v1.0.3

4
go.sum
View File

@ -11,8 +11,8 @@ github.com/hajimehoshi/go-mp3 v0.3.3 h1:cWnfRdpye2m9ElSoVqneYRcpt/l3ijttgjMeQh+r
github.com/hajimehoshi/go-mp3 v0.3.3/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM= github.com/hajimehoshi/go-mp3 v0.3.3/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1 h1:7cJz/zRQV4aJvMSSRqzN2TImoVVMpE0BCY4nrNJaDOM= github.com/hajimehoshi/oto v0.6.1 h1:7cJz/zRQV4aJvMSSRqzN2TImoVVMpE0BCY4nrNJaDOM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI= github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
github.com/hajimehoshi/oto/v2 v2.2.0-alpha.3.0.20220627134823-65dbd988610c h1:hHMvizeDtTgaLcvh78CvzVi82yrYkUJsr81vfRBeeg0= github.com/hajimehoshi/oto/v2 v2.2.0-alpha.3.0.20220630171212-19c3101d3014 h1:CzwUmBzSfGTCrUXwnuhEbZf3QFzr1qYolBEYE4VxvLI=
github.com/hajimehoshi/oto/v2 v2.2.0-alpha.3.0.20220627134823-65dbd988610c/go.mod h1:9i0oYbpJ8BhVGkXDKdXKfFthX1JUNfXjeTp944W8TGM= github.com/hajimehoshi/oto/v2 v2.2.0-alpha.3.0.20220630171212-19c3101d3014/go.mod h1:9i0oYbpJ8BhVGkXDKdXKfFthX1JUNfXjeTp944W8TGM=
github.com/jakecoffman/cp v1.2.0 h1:ZdCFqHglNYJibiIeRvpAktJ7ZuWUnh0cnBsZNKVbY3A= github.com/jakecoffman/cp v1.2.0 h1:ZdCFqHglNYJibiIeRvpAktJ7ZuWUnh0cnBsZNKVbY3A=
github.com/jakecoffman/cp v1.2.0/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg= github.com/jakecoffman/cp v1.2.0/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
github.com/jezek/xgb v1.0.1 h1:YUGhxps0aR7J2Xplbs23OHnV1mWaxFVcOl9b+1RQkt8= github.com/jezek/xgb v1.0.1 h1:YUGhxps0aR7J2Xplbs23OHnV1mWaxFVcOl9b+1RQkt8=