-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_manager.go
60 lines (50 loc) · 1.13 KB
/
audio_manager.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
package main
import (
"os"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
)
type AudioManager struct {
assets map[string]*audio.Player
currentBGM *audio.Player
game *Game
}
func (am *AudioManager) Load(key string, path string) {
fd, err := os.Open(path)
if err != nil {
return
}
stream, err := vorbis.DecodeWithSampleRate(44100, fd)
if err != nil {
return
}
am.assets[key], err = audio.NewPlayer(GetAudioContext(), stream)
if err != nil {
return
}
}
func (am *AudioManager) Play(key string) {
if am.game.volumeMusic > 0 {
am.assets[key].Pause()
am.assets[key].Rewind()
am.assets[key].Play()
}
}
func (am *AudioManager) PlayBackgroundMusic(key string) {
/*
if am.currentBGM != nil {
am.currentBGM.Pause()
am.currentBGM.Rewind()
}
am.currentBGM = am.game.audioManager.assets[key]
am.currentBGM.Pause()
am.currentBGM.Rewind()
am.currentBGM.Play()
*/
}
func NewAudioManager(g *Game) *AudioManager {
am := new(AudioManager)
am.game = g
am.assets = make(map[string]*audio.Player)
return am
}