audio: Bug fix: Handle touch event for iOS (#296)

This commit is contained in:
Hajime Hoshi 2016-11-30 03:25:00 +09:00
parent 604827ed3a
commit 741ce9c7b8

View File

@ -18,6 +18,7 @@ package driver
import (
"errors"
"strings"
"github.com/gopherjs/gopherjs/js"
)
@ -31,6 +32,14 @@ type Player struct {
context *js.Object
}
func isIOS() bool {
ua := js.Global.Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "iPhone") {
return false
}
return true
}
func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
class := js.Global.Get("AudioContext")
if class == js.Undefined {
@ -46,6 +55,13 @@ func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
bufferedData: []byte{},
context: class.New(),
}
// iOS requires touch event to use AudioContext.
if isIOS() {
js.Global.Get("document").Call("addEventListener", "touchend", func() {
p.context.Call("createBufferSource").Call("start", 0)
p.positionInSamples = int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
})
}
p.positionInSamples = int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
return p, nil
}