-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaudio_js.go
112 lines (91 loc) · 2.19 KB
/
audio_js.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// +build netgo
package audio
import (
"log"
"time"
"honnef.co/go/js/dom"
)
type Player struct {
player *dom.HTMLAudioElement
duration time.Duration
src string
format int
samplesPerSecond int64
}
type Resource interface {
Url() string
}
func notImplemented(msg string) {
log.Println("[WARNING] " + msg + " is not implemented on Gopherjs")
}
//TODO: Use format and samples per second
func NewPlayer(src string, format int, samplesPerSecond int64) (*Player, error) {
player := dom.GetWindow().Document().CreateElement("audio").(*dom.HTMLAudioElement)
player.SetAttribute("src", src)
player.AddEventListener("loadeddata", false, func(ev dom.Event) {
//TODO: somehow save this somewhere in the player
_, err := time.ParseDuration(player.Underlying().Get("duration").String() + "s")
if err != nil {
log.Println("[ERROR] Failed to get audio duration: " + err.Error())
}
})
return &Player{player: player, src: src, format: format, samplesPerSecond: samplesPerSecond}, nil
}
func NewSimplePlayer(src string) (*Player, error) {
return NewPlayer(src, 0, 0)
}
func (p *Player) Close() error {
notImplemented("close")
return nil
}
func (p *Player) Current() time.Duration {
cur := p.player.Underlying().Get("currentTime").String()
dur, _ := time.ParseDuration(cur + "s")
return dur
}
func (p *Player) Pause() error {
p.player.Pause()
return nil
}
func (p *Player) Play() error {
p.player.Play()
return nil
}
func (p *Player) Seek(offset time.Duration) error {
notImplemented("seek")
return nil
}
func (p *Player) SetVolume(vol float64) {
if vol > 1 {
vol = 1
}
p.player.Underlying().Set("volume", vol)
}
func (p *Player) State() State {
if p.player.Paused {
return Paused
} else {
return Playing
}
}
func (p *Player) Stop() error {
notImplemented("stop")
return nil
}
func (p *Player) Total(bg bool) time.Duration {
//TODO: Use the bg parameter
return p.duration
}
func (p *Player) Volume() float64 {
return p.player.Underlying().Get("volume").Float()
}
func (p *Player) Rewind() {
notImplemented("rewind")
}
func (p *Player) PlaySources() {
notImplemented("playSources")
}
func Preload() error {
notImplemented("preload")
return nil
}