mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-13 12:32:05 +01:00
audio/internal/readdriver: Bug fix: Implement Suspend/Resume for browsers
Closes #1630
This commit is contained in:
parent
44e96f2377
commit
29eade9b4a
@ -127,9 +127,15 @@ func NewContext(sampleRate int) *Context {
|
|||||||
h := getHook()
|
h := getHook()
|
||||||
h.OnSuspendAudio(func() {
|
h.OnSuspendAudio(func() {
|
||||||
c.semaphore <- struct{}{}
|
c.semaphore <- struct{}{}
|
||||||
|
if s, ok := np.(interface{ suspend() }); ok {
|
||||||
|
s.suspend()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
h.OnResumeAudio(func() {
|
h.OnResumeAudio(func() {
|
||||||
<-c.semaphore
|
<-c.semaphore
|
||||||
|
if s, ok := np.(interface{ resume() }); ok {
|
||||||
|
s.resume()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
h.AppendHookOnBeforeUpdate(func() error {
|
h.AppendHookOnBeforeUpdate(func() error {
|
||||||
|
@ -21,6 +21,8 @@ import (
|
|||||||
type Context interface {
|
type Context interface {
|
||||||
NewPlayer(io.Reader) Player
|
NewPlayer(io.Reader) Player
|
||||||
MaxBufferSize() int
|
MaxBufferSize() int
|
||||||
|
Suspend()
|
||||||
|
Resume()
|
||||||
io.Closer
|
io.Closer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,6 +139,14 @@ func (c *context) MaxBufferSize() int {
|
|||||||
return c.oneBufferSize() * 2
|
return c.oneBufferSize() * 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *context) Suspend() {
|
||||||
|
c.audioContext.Call("suspend")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *context) Resume() {
|
||||||
|
c.audioContext.Call("resume")
|
||||||
|
}
|
||||||
|
|
||||||
func (p *player) Pause() {
|
func (p *player) Pause() {
|
||||||
if p.state != playerPlay {
|
if p.state != playerPlay {
|
||||||
return
|
return
|
||||||
@ -299,6 +307,14 @@ func (w *go2cppDriverWrapper) MaxBufferSize() int {
|
|||||||
return w.c.MaxBufferSize()
|
return w.c.MaxBufferSize()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *go2cppDriverWrapper) Suspend() {
|
||||||
|
// Do nothing so far.
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *go2cppDriverWrapper) Resume() {
|
||||||
|
// Do nothing so far.
|
||||||
|
}
|
||||||
|
|
||||||
func (w *go2cppDriverWrapper) Close() error {
|
func (w *go2cppDriverWrapper) Close() error {
|
||||||
return w.c.Close()
|
return w.c.Close()
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,20 @@ func (f *readerPlayerFactory) newPlayerImpl(context *Context, src io.Reader) (pl
|
|||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *readerPlayerFactory) suspend() {
|
||||||
|
if f.context == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f.context.Suspend()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *readerPlayerFactory) resume() {
|
||||||
|
if f.context == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f.context.Resume()
|
||||||
|
}
|
||||||
|
|
||||||
func (p *readerPlayer) ensurePlayer() error {
|
func (p *readerPlayer) ensurePlayer() error {
|
||||||
// Initialize the underlying player lazily to enable calling NewContext in an 'init' function.
|
// Initialize the underlying player lazily to enable calling NewContext in an 'init' function.
|
||||||
// Accessing the underlying player functions requires the environment to be already initialized,
|
// Accessing the underlying player functions requires the environment to be already initialized,
|
||||||
|
Loading…
Reference in New Issue
Block a user