mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
audio: Bug fix: Handle touch event for iOS (#296)
This commit is contained in:
parent
604827ed3a
commit
741ce9c7b8
@ -18,6 +18,7 @@ package driver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gopherjs/gopherjs/js"
|
"github.com/gopherjs/gopherjs/js"
|
||||||
)
|
)
|
||||||
@ -31,6 +32,14 @@ type Player struct {
|
|||||||
context *js.Object
|
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) {
|
func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
|
||||||
class := js.Global.Get("AudioContext")
|
class := js.Global.Get("AudioContext")
|
||||||
if class == js.Undefined {
|
if class == js.Undefined {
|
||||||
@ -46,6 +55,13 @@ func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
|
|||||||
bufferedData: []byte{},
|
bufferedData: []byte{},
|
||||||
context: class.New(),
|
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))
|
p.positionInSamples = int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user