audio: Bug fix: old API in JavaScript

This commit is contained in:
Hajime Hoshi 2016-04-03 06:36:04 +09:00
parent 65a8d4013a
commit 8d6ed85145

View File

@ -17,6 +17,7 @@
package audio package audio
import ( import (
"errors"
"io" "io"
"runtime" "runtime"
@ -32,10 +33,10 @@ type player struct {
bufferSource *js.Object bufferSource *js.Object
} }
func startPlaying(src io.Reader, sampleRate int) (*player, error) { func startPlaying(src io.Reader, sampleRate int) error {
// 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 nil, nil return nil
} }
class := js.Global.Get("AudioContext") class := js.Global.Get("AudioContext")
@ -43,7 +44,7 @@ func startPlaying(src io.Reader, sampleRate int) (*player, error) {
class = js.Global.Get("webkitAudioContext") class = js.Global.Get("webkitAudioContext")
} }
if class == js.Undefined { if class == js.Undefined {
panic("audio: audio couldn't be initialized") return errors.New("audio: audio couldn't be initialized")
} }
p := &player{ p := &player{
src: src, src: src,
@ -52,10 +53,7 @@ func startPlaying(src io.Reader, sampleRate int) (*player, error) {
context: class.New(), context: class.New(),
} }
p.positionInSamples = int64(p.context.Get("currentTime").Float() * float64(p.sampleRate)) p.positionInSamples = int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
if err := p.start(); err != nil { return p.start()
return nil, err
}
return p, nil
} }
func toLR(data []byte) ([]int16, []int16) { func toLR(data []byte) ([]int16, []int16) {