-
Notifications
You must be signed in to change notification settings - Fork 1
/
music.go
75 lines (62 loc) · 1.24 KB
/
music.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
package main
import (
"github.com/ketMix/retromancer/states"
"github.com/hajimehoshi/ebiten/v2/audio"
)
type MusicPlayer struct {
player *audio.Player
song states.Song
loop bool
volume float64
}
func (m *MusicPlayer) Play(s states.Song) (err error) {
if m.player != nil {
// Do nothing if the song is already playing
if m.song == s && m.player.IsPlaying() {
return
}
m.player.Pause()
m.player.Close()
}
m.player, err = audio.CurrentContext().NewPlayer(s.Stream())
if err != nil {
return
}
m.player.SetVolume(m.volume)
// Seek to 0 in case the underlying song was partially played.
m.player.Seek(0)
m.player.Play()
m.song = s
return
}
func (m *MusicPlayer) Update() {
if m.player != nil && m.song != nil && !m.player.IsPlaying() {
m.player.Seek(0)
m.player.Play()
}
}
func (m *MusicPlayer) Pause() {
if m.player != nil {
m.player.Pause()
}
}
func (m *MusicPlayer) Resume() {
if m.player != nil {
m.player.Play()
}
}
func (m *MusicPlayer) Loop() bool {
return m.loop
}
func (m *MusicPlayer) SetLoop(loop bool) {
m.loop = loop
}
func (m *MusicPlayer) Volume() float64 {
return m.volume
}
func (m *MusicPlayer) SetVolume(vol float64) {
m.volume = vol
if m.player != nil {
m.player.SetVolume(m.volume)
}
}