audio: Add Context.IsReady

Fixes #717
This commit is contained in:
Hajime Hoshi 2018-10-13 22:41:37 +09:00
parent b6f7a0043c
commit d14202ef7b
4 changed files with 23 additions and 5 deletions

View File

@ -170,6 +170,7 @@ type Context struct {
players *players players *players
sampleRate int sampleRate int
err error err error
ready bool
m sync.Mutex m sync.Mutex
} }
@ -266,15 +267,27 @@ func (c *Context) loop() {
case <-suspendCh: case <-suspendCh:
<-resumeCh <-resumeCh
default: default:
const n = 2048 if _, err := io.CopyN(p, c.players, 2048); err != nil {
if _, err := io.CopyN(p, c.players, n); err != nil {
c.err = err c.err = err
return return
} }
c.m.Lock()
c.ready = true
c.m.Unlock()
} }
} }
} }
// IsReady returns a boolean value indicating whether the audio is ready or not.
//
// On some browsers, user interaction like click or pressing keys is required to start audio.
func (c *Context) IsReady() bool {
c.m.Lock()
r := c.ready
c.m.Unlock()
return r
}
// Update is deprecated as of 1.6.0-alpha. // Update is deprecated as of 1.6.0-alpha.
// //
// As of 1.6.0-alpha, Update always returns nil and does nothing related to updating the state. // As of 1.6.0-alpha, Update always returns nil and does nothing related to updating the state.

View File

@ -139,7 +139,7 @@ var (
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
// Play notes for each half second. // Play notes for each half second.
if frames%30 == 0 { if frames%30 == 0 && audioContext.IsReady() {
currentNote = playNote(scoreIndex) currentNote = playNote(scoreIndex)
scoreIndex++ scoreIndex++
scoreIndex %= len(score) scoreIndex %= len(score)
@ -151,11 +151,14 @@ func update(screen *ebiten.Image) error {
} }
msg := "Note: " msg := "Note: "
if currentNote == 'R' { if currentNote == 'R' || currentNote == 0 {
msg += "-" msg += "-"
} else { } else {
msg += string(currentNote) msg += string(currentNote)
} }
if !audioContext.IsReady() {
msg += "\n\n(If the audio doesn't start,\n click the screen or press keys)"
}
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
return nil return nil
} }

2
go.mod
View File

@ -7,7 +7,7 @@ require (
github.com/gopherjs/gopherwasm v1.0.0 github.com/gopherjs/gopherwasm v1.0.0
github.com/hajimehoshi/bitmapfont v1.1.1 github.com/hajimehoshi/bitmapfont v1.1.1
github.com/hajimehoshi/go-mp3 v0.1.1 github.com/hajimehoshi/go-mp3 v0.1.1
github.com/hajimehoshi/oto v0.2.0 github.com/hajimehoshi/oto v0.2.1-0.20181013145203-3a9bb05c78a0
github.com/jakecoffman/cp v0.1.0 github.com/jakecoffman/cp v0.1.0
github.com/jfreymuth/oggvorbis v1.0.0 github.com/jfreymuth/oggvorbis v1.0.0
github.com/jfreymuth/vorbis v1.0.0 // indirect github.com/jfreymuth/vorbis v1.0.0 // indirect

2
go.sum
View File

@ -24,6 +24,8 @@ github.com/hajimehoshi/oto v0.1.4 h1:YixDDnNhUjtrYP84YXISGWhQUN2VJfVIgKUeSNMWtxQ
github.com/hajimehoshi/oto v0.1.4/go.mod h1:0ZepxT+2KLDrCm1gdkKBCQCxr+8fgQqoh0I7g+kr040= github.com/hajimehoshi/oto v0.1.4/go.mod h1:0ZepxT+2KLDrCm1gdkKBCQCxr+8fgQqoh0I7g+kr040=
github.com/hajimehoshi/oto v0.2.0 h1:7JRO4/b+/xjWdV9ccUs35xcYTDrzRHxs5qnxeMHj7CM= github.com/hajimehoshi/oto v0.2.0 h1:7JRO4/b+/xjWdV9ccUs35xcYTDrzRHxs5qnxeMHj7CM=
github.com/hajimehoshi/oto v0.2.0/go.mod h1:0ZepxT+2KLDrCm1gdkKBCQCxr+8fgQqoh0I7g+kr040= github.com/hajimehoshi/oto v0.2.0/go.mod h1:0ZepxT+2KLDrCm1gdkKBCQCxr+8fgQqoh0I7g+kr040=
github.com/hajimehoshi/oto v0.2.1-0.20181013145203-3a9bb05c78a0 h1:Ruf7x6Tkg6lx6fwHO8a38eR/cf+2GEX+9tAyJnqV8jc=
github.com/hajimehoshi/oto v0.2.1-0.20181013145203-3a9bb05c78a0/go.mod h1:0ZepxT+2KLDrCm1gdkKBCQCxr+8fgQqoh0I7g+kr040=
github.com/jakecoffman/cp v0.1.0 h1:sgSYEGUgfwiT447fRjloa2c5b6UyYP+7muR3gQK+Ep0= github.com/jakecoffman/cp v0.1.0 h1:sgSYEGUgfwiT447fRjloa2c5b6UyYP+7muR3gQK+Ep0=
github.com/jakecoffman/cp v0.1.0/go.mod h1:a3xPx9N8RyFAACD644t2dj/nK4SuLg1v+jL61m2yVo4= github.com/jakecoffman/cp v0.1.0/go.mod h1:a3xPx9N8RyFAACD644t2dj/nK4SuLg1v+jL61m2yVo4=
github.com/jfreymuth/oggvorbis v1.0.0 h1:aOpiihGrFLXpsh2osOlEvTcg5/aluzGQeC7m3uYWOZ0= github.com/jfreymuth/oggvorbis v1.0.0 h1:aOpiihGrFLXpsh2osOlEvTcg5/aluzGQeC7m3uYWOZ0=