audio: Remove Init

This commit is contained in:
Hajime Hoshi 2016-02-11 17:16:34 +09:00
parent 8e43d1047b
commit 84d50f7714
4 changed files with 11 additions and 30 deletions

View File

@ -15,7 +15,6 @@
package ebiten package ebiten
import ( import (
"github.com/hajimehoshi/ebiten/internal/audio"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl" "github.com/hajimehoshi/ebiten/internal/graphics/opengl"
"github.com/hajimehoshi/ebiten/internal/ui" "github.com/hajimehoshi/ebiten/internal/ui"
) )
@ -27,5 +26,4 @@ func init() {
ui.ExecOnUIThread(func() { ui.ExecOnUIThread(func() {
glContext = opengl.NewContext() glContext = opengl.NewContext()
}) })
audio.Init()
} }

View File

@ -18,10 +18,6 @@ import (
"io" "io"
) )
func Init() {
initialize()
}
func Play(src io.ReadSeeker, sampleRate int) error { func Play(src io.ReadSeeker, sampleRate int) error {
return play(src, sampleRate) return play(src, sampleRate)
} }

View File

@ -17,6 +17,7 @@
package audio package audio
import ( import (
"errors"
"io" "io"
"io/ioutil" "io/ioutil"
@ -70,6 +71,11 @@ func (a *audioProcessor) play() error {
} }
func play(src io.ReadSeeker, sampleRate int) error { func play(src io.ReadSeeker, sampleRate int) error {
if context == nil {
if !initialize() {
return errors.New("audio couldn't be initialized")
}
}
a := &audioProcessor{ a := &audioProcessor{
src: src, src: src,
sampleRate: sampleRate, sampleRate: sampleRate,
@ -78,10 +84,10 @@ func play(src io.ReadSeeker, sampleRate int) error {
return a.play() return a.play()
} }
func initialize() { func initialize() bool {
// Do nothing in node.js. // Do nothing in node.js.
if js.Global.Get("require") != js.Undefined { if js.Global.Get("require") != js.Undefined {
return return false
} }
class := js.Global.Get("AudioContext") class := js.Global.Get("AudioContext")
@ -89,7 +95,8 @@ func initialize() {
class = js.Global.Get("webkitAudioContext") class = js.Global.Get("webkitAudioContext")
} }
if class == js.Undefined { if class == js.Undefined {
return return false
} }
context = class.New() context = class.New()
return true
} }

View File

@ -18,13 +18,10 @@ package audio
import ( import (
"io" "io"
"time"
"golang.org/x/mobile/exp/audio" "golang.org/x/mobile/exp/audio"
) )
var players = map[*audio.Player]struct{}{}
type readSeekCloser struct { type readSeekCloser struct {
io.ReadSeeker io.ReadSeeker
} }
@ -40,24 +37,7 @@ func play(src io.ReadSeeker, sampleRate int) error {
if err != nil { if err != nil {
return err return err
} }
players[p] = struct{}{}
return p.Play() return p.Play()
} }
func initialize() { // TODO: Implement Close method
go func() {
for {
deleted := []*audio.Player{}
for p, _ := range players {
if p.State() == audio.Stopped {
p.Close()
deleted = append(deleted, p)
}
}
for _, p := range deleted {
delete(players, p)
}
time.Sleep(1 * time.Millisecond)
}
}()
}