-
-
Notifications
You must be signed in to change notification settings - Fork 544
/
player.go
51 lines (41 loc) · 907 Bytes
/
player.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
package asset
import (
"github.com/hajimehoshi/go-mp3"
"github.com/hajimehoshi/oto"
"io"
"log"
)
type AudioPlayer struct {
player *oto.Player
beep []byte
}
func NewAudioPlayer() *AudioPlayer {
bytes, err := Asset("quindar-tone.mp3")
if err != nil {
log.Fatal("Failed to find audio file")
}
player, err := oto.NewPlayer(44100, 2, 2, 8192)
if err != nil {
// it is expected to fail when some of the system
// libraries are not available (e.g. libasound2)
// it is not the main functionality of the application,
// so we allow startup in no-sound mode
return nil
}
return &AudioPlayer{
player: player,
beep: bytes,
}
}
func (a *AudioPlayer) Beep() {
decoder, err := mp3.NewDecoder(NewAssetFile(a.beep))
if err != nil {
panic(err)
}
if _, err := io.Copy(a.player, decoder); err != nil {
panic(err)
}
}
func (a *AudioPlayer) Close() {
_ = a.player.Close()
}