diff --git a/audio/audio.go b/audio/audio.go index 1547dcab8..569a128f0 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -250,10 +250,7 @@ func (c *Context) loop() { // e.g. a variable for JVM on Android might not be set. <-c.initCh - // The buffer size is 1/15 sec. - // It looks like 1/20 sec is too short for Android. - s := c.sampleRate * channelNum * bytesPerSample / 15 - p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, s) + p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, c.bufferSize()) if err != nil { c.errCh <- err return diff --git a/audio/buffersize.go b/audio/buffersize.go new file mode 100644 index 000000000..70c469544 --- /dev/null +++ b/audio/buffersize.go @@ -0,0 +1,22 @@ +// Copyright 2017 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !js +// +build !ios + +package audio + +func (c *Context) bufferSize() int { + return c.sampleRate * channelNum * bytesPerSample / 30 +} diff --git a/audio/buffersize_ios.go b/audio/buffersize_ios.go new file mode 100644 index 000000000..5c60e20b3 --- /dev/null +++ b/audio/buffersize_ios.go @@ -0,0 +1,22 @@ +// Copyright 2017 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build ios + +package audio + +func (c *Context) bufferSize() int { + // TODO: Fix after oto uses HAL on Darwin. + return c.sampleRate * channelNum * bytesPerSample / 20 +} diff --git a/audio/buffersize_js.go b/audio/buffersize_js.go new file mode 100644 index 000000000..1a1399084 --- /dev/null +++ b/audio/buffersize_js.go @@ -0,0 +1,29 @@ +// Copyright 2017 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build js + +package audio + +import ( + "github.com/hajimehoshi/ebiten/internal/web" +) + +func (c *Context) bufferSize() int { + n := 30 + if web.IsMobileBrowser() { + n = 10 + } + return c.sampleRate * channelNum * bytesPerSample / n +} diff --git a/internal/restorable/enabled_js.go b/internal/restorable/enabled_js.go index d318fdbba..2013c900d 100644 --- a/internal/restorable/enabled_js.go +++ b/internal/restorable/enabled_js.go @@ -17,32 +17,11 @@ package restorable import ( - "strings" - - "github.com/gopherjs/gopherjs/js" + "github.com/hajimehoshi/ebiten/internal/web" ) -func isIOS() bool { - ua := js.Global.Get("navigator").Get("userAgent").String() - if !strings.Contains(ua, "iPhone") { - return false - } - return true -} - -func isAndroidChrome() bool { - ua := js.Global.Get("navigator").Get("userAgent").String() - if !strings.Contains(ua, "Android") { - return false - } - if !strings.Contains(ua, "Chrome") { - return false - } - return true -} - func init() { - if isIOS() || isAndroidChrome() { + if web.IsMobileBrowser() { restoringEnabled = false } } diff --git a/internal/web/mobile.go b/internal/web/mobile.go new file mode 100644 index 000000000..c9a2e4f5d --- /dev/null +++ b/internal/web/mobile.go @@ -0,0 +1,46 @@ +// Copyright 2017 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build js + +package web + +import ( + "strings" + + "github.com/gopherjs/gopherjs/js" +) + +func isIOS() bool { + ua := js.Global.Get("navigator").Get("userAgent").String() + if !strings.Contains(ua, "iPhone") { + return false + } + return true +} + +func isAndroidChrome() bool { + ua := js.Global.Get("navigator").Get("userAgent").String() + if !strings.Contains(ua, "Android") { + return false + } + if !strings.Contains(ua, "Chrome") { + return false + } + return true +} + +func IsMobileBrowser() bool { + return isIOS() || isAndroidChrome() +}