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
import (
"github.com/hajimehoshi/ebiten/internal/audio"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
"github.com/hajimehoshi/ebiten/internal/ui"
)
@ -27,5 +26,4 @@ func init() {
ui.ExecOnUIThread(func() {
glContext = opengl.NewContext()
})
audio.Init()
}

View File

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

View File

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

View File

@ -18,13 +18,10 @@ package audio
import (
"io"
"time"
"golang.org/x/mobile/exp/audio"
)
var players = map[*audio.Player]struct{}{}
type readSeekCloser struct {
io.ReadSeeker
}
@ -40,24 +37,7 @@ func play(src io.ReadSeeker, sampleRate int) error {
if err != nil {
return err
}
players[p] = struct{}{}
return p.Play()
}
func initialize() {
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)
}
}()
}
// TODO: Implement Close method