mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +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.OnSuspendAudio(func() {
|
||||
c.semaphore <- struct{}{}
|
||||
if s, ok := np.(interface{ suspend() }); ok {
|
||||
s.suspend()
|
||||
}
|
||||
})
|
||||
h.OnResumeAudio(func() {
|
||||
<-c.semaphore
|
||||
if s, ok := np.(interface{ resume() }); ok {
|
||||
s.resume()
|
||||
}
|
||||
})
|
||||
|
||||
h.AppendHookOnBeforeUpdate(func() error {
|
||||
|
@ -21,6 +21,8 @@ import (
|
||||
type Context interface {
|
||||
NewPlayer(io.Reader) Player
|
||||
MaxBufferSize() int
|
||||
Suspend()
|
||||
Resume()
|
||||
io.Closer
|
||||
}
|
||||
|
||||
|
@ -139,6 +139,14 @@ func (c *context) MaxBufferSize() int {
|
||||
return c.oneBufferSize() * 2
|
||||
}
|
||||
|
||||
func (c *context) Suspend() {
|
||||
c.audioContext.Call("suspend")
|
||||
}
|
||||
|
||||
func (c *context) Resume() {
|
||||
c.audioContext.Call("resume")
|
||||
}
|
||||
|
||||
func (p *player) Pause() {
|
||||
if p.state != playerPlay {
|
||||
return
|
||||
@ -299,6 +307,14 @@ func (w *go2cppDriverWrapper) MaxBufferSize() int {
|
||||
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 {
|
||||
return w.c.Close()
|
||||
}
|
||||
|
@ -60,6 +60,20 @@ func (f *readerPlayerFactory) newPlayerImpl(context *Context, src io.Reader) (pl
|
||||
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 {
|
||||
// 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,
|
||||
|
Loading…
Reference in New Issue
Block a user