-
Notifications
You must be signed in to change notification settings - Fork 5
/
player.go
62 lines (53 loc) · 1.28 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"errors"
"io"
"os/exec"
)
var commandList = map[string]string{
"play": "p",
"pause": "p",
"nextsubs": "m",
"prevsubs": "n",
"stop": "q",
"up": "\x1b[A",
"down": "\x1b[B",
"forward": "\x1b[C",
"backward": "\x1b[D",
}
// Player is the struct that controls the playback on the omxplayer
// using an exec.Cmd command
type Player struct {
Command *exec.Cmd
PipeIn io.WriteCloser
Playing bool
}
// Start will Start omxplayer playback for a given filename
// it will stop any playback that is currently running
func (p *Player) Start(options []string) error {
var err error
if p.Playing {
p.SendCommand("stop")
p.Playing = false
}
p.Command = exec.Command("omxplayer", options...)
p.PipeIn, err = p.Command.StdinPipe()
if err != nil {
return err
}
p.Playing = true
err = p.Command.Start()
if err != nil {
p.Playing = false
}
return err
}
// SendCommand sends command to omxplayer using the pipe from the struct.
// Command can be one of: "play", "pause", "subs", "stop", "backward", "forward"
func (p *Player) SendCommand(command string) error {
if _, ok := commandList[command]; ok {
_, err := p.PipeIn.Write([]byte(commandList[command]))
return err
}
return errors.New("player.sendcommand: unknown command " + command)
}