audio: Bug fix: oto.Player must not be called on init

This commit is contained in:
Hajime Hoshi 2017-07-12 02:24:54 +09:00
parent 15f5d2a2cc
commit 3454b32cb6

View File

@ -173,6 +173,8 @@ func (p *players) hasSource(src ReadSeekCloser) bool {
type Context struct { type Context struct {
players *players players *players
errCh chan error errCh chan error
initCh chan struct{}
initedCh chan struct{}
pingCount int pingCount int
sampleRate int sampleRate int
frames int64 frames int64
@ -200,6 +202,8 @@ func NewContext(sampleRate int) (*Context, error) {
c := &Context{ c := &Context{
sampleRate: sampleRate, sampleRate: sampleRate,
errCh: make(chan error, 1), errCh: make(chan error, 1),
initCh: make(chan struct{}),
initedCh: make(chan struct{}),
} }
theContext = c theContext = c
c.players = &players{ c.players = &players{
@ -228,6 +232,11 @@ func (c *Context) Frame() int64 {
// Internal Only? // Internal Only?
func (c *Context) Ping() { func (c *Context) Ping() {
if c.initCh != nil {
close(c.initCh)
c.initCh = nil
<-c.initedCh
}
c.m.Lock() c.m.Lock()
c.pingCount = 5 c.pingCount = 5
c.m.Unlock() c.m.Unlock()
@ -239,6 +248,7 @@ func (c *Context) loop() {
// but if Ebiten is used for a shared library, the timing when init functions are called // but if Ebiten is used for a shared library, the timing when init functions are called
// is unexpectable. // is unexpectable.
// e.g. a variable for JVM on Android might not be set. // e.g. a variable for JVM on Android might not be set.
<-c.initCh
// The buffer size is 1/15 sec. // The buffer size is 1/15 sec.
// It looks like 1/20 sec is too short for Android. // It looks like 1/20 sec is too short for Android.
@ -246,9 +256,13 @@ func (c *Context) loop() {
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, s) p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, s)
if err != nil { if err != nil {
c.errCh <- err c.errCh <- err
return
} }
defer p.Close() defer p.Close()
close(c.initedCh)
c.initedCh = nil
for { for {
c.m.Lock() c.m.Lock()
c.framesReadOnly = c.frames c.framesReadOnly = c.frames