From 741ce9c7b811baee987756c771529442ede9d1bc Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 30 Nov 2016 03:25:00 +0900 Subject: [PATCH] audio: Bug fix: Handle touch event for iOS (#296) --- audio/internal/driver/driver_js.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 }