forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.go
More file actions
118 lines (97 loc) · 2.31 KB
/
player.go
File metadata and controls
118 lines (97 loc) · 2.31 KB
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
113
114
115
116
117
118
package main
import (
"github.com/yourok/go-mpv/mpv"
)
const (
PlayerStopped = 0
PlayerPlaying = 1
PlayerPaused = 2
)
type QueueItem struct {
Uri string
Title string
Artist string
Duration int
}
type Player struct {
Instance *mpv.Mpv
EventChannel chan *mpv.Event
Queue []QueueItem
ReplaceInProgress bool
}
func eventListener(m *mpv.Mpv) chan *mpv.Event {
c := make(chan *mpv.Event)
go func() {
for {
e := m.WaitEvent(1)
c <- e
}
}()
return c
}
func InitPlayer() (*Player, error) {
mpvInstance := mpv.Create()
// TODO figure out what other mpv options we need
mpvInstance.SetOptionString("audio-display", "no")
mpvInstance.SetOptionString("video", "no")
err := mpvInstance.Initialize()
if err != nil {
mpvInstance.TerminateDestroy()
return nil, err
}
return &Player{mpvInstance, eventListener(mpvInstance), nil, false}, nil
}
func (p *Player) PlayNextTrack() {
if len(p.Queue) > 0 {
p.Instance.Command([]string{"loadfile", p.Queue[0].Uri})
}
}
func (p *Player) Play(uri string, title string, artist string, duration int) {
p.Queue = []QueueItem{QueueItem{uri, title, artist, duration}}
p.ReplaceInProgress = true
p.Instance.Command([]string{"loadfile", uri})
}
func (p *Player) Stop() {
p.Instance.Command([]string{"stop"})
}
func (p *Player) IsSongLoaded() bool {
idle, _ := p.Instance.GetProperty("idle-active", mpv.FORMAT_FLAG)
return !idle.(bool)
}
func (p *Player) IsPaused() bool {
pause, _ := p.Instance.GetProperty("pause", mpv.FORMAT_FLAG)
return pause.(bool)
}
func (p *Player) Pause() int {
loaded := p.IsSongLoaded()
pause := p.IsPaused()
if loaded {
if pause {
p.Instance.SetProperty("pause", mpv.FORMAT_FLAG, false)
return PlayerPlaying
} else {
p.Instance.SetProperty("pause", mpv.FORMAT_FLAG, true)
return PlayerPaused
}
} else {
if len(p.Queue) != 0 {
p.Instance.Command([]string{"loadfile", p.Queue[0].Uri})
return PlayerPlaying
} else {
return PlayerStopped
}
}
}
func (p *Player) AdjustVolume(increment int64) {
volume, _ := p.Instance.GetProperty("volume", mpv.FORMAT_INT64)
if volume == nil {
return
}
nevVolume := volume.(int64) + increment
if nevVolume > 100 {
nevVolume = 100
} else if nevVolume < 0 {
nevVolume = 0
}
p.Instance.SetProperty("volume", mpv.FORMAT_INT64, nevVolume)
}