-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayback_control.go
80 lines (68 loc) · 1.66 KB
/
playback_control.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
package main
import (
"sync"
"time"
"github.com/bwmarrin/discordgo"
)
type PlaybackControl struct {
stopChan chan struct{}
isPlaying bool
mu sync.Mutex
currentFile string
voiceConnection *discordgo.VoiceConnection
}
var (
playbackControl = &PlaybackControl{
stopChan: make(chan struct{}),
}
)
func (pc *PlaybackControl) Stop() {
pc.mu.Lock()
defer pc.mu.Unlock()
if pc.isPlaying {
close(pc.stopChan)
pc.stopChan = make(chan struct{})
pc.isPlaying = false
if pc.voiceConnection != nil {
pc.voiceConnection.Speaking(false)
time.Sleep(500 * time.Millisecond)
pc.voiceConnection.Disconnect()
pc.voiceConnection = nil
}
}
}
func (pc *PlaybackControl) SetVoiceConnection(vc *discordgo.VoiceConnection) {
pc.mu.Lock()
defer pc.mu.Unlock()
pc.voiceConnection = vc
}
func (pc *PlaybackControl) Start(filename string) {
pc.mu.Lock()
defer pc.mu.Unlock()
pc.currentFile = filename
pc.isPlaying = true
}
func (pc *PlaybackControl) Finish() {
pc.mu.Lock()
defer pc.mu.Unlock()
pc.isPlaying = false
pc.currentFile = ""
if pc.voiceConnection != nil {
pc.voiceConnection.Speaking(false)
pc.voiceConnection.Disconnect()
pc.voiceConnection = nil
}
}
func (pc *PlaybackControl) IsPlaying() bool {
pc.mu.Lock()
defer pc.mu.Unlock()
return pc.isPlaying
}
func (pc *PlaybackControl) GetStopChan() chan struct{} {
return pc.stopChan
}
func (pc *PlaybackControl) GetCurrentFile() string {
pc.mu.Lock()
defer pc.mu.Unlock()
return pc.currentFile
}