diff --git a/audio/internal/driver/driver_js.go b/audio/internal/driver/driver_js.go index 859eef1aa..b204b3328 100644 --- a/audio/internal/driver/driver_js.go +++ b/audio/internal/driver/driver_js.go @@ -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 }