-
Notifications
You must be signed in to change notification settings - Fork 0
/
omxplayer.go
87 lines (72 loc) · 1.58 KB
/
omxplayer.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
81
82
83
84
85
86
87
package omxplayer
import (
"errors"
"io/ioutil"
"os"
"os/exec"
"time"
dbus "github.com/hoffoo/go.dbus"
)
var sdbus *dbus.Object
type OmxPlayer struct {
sdbus *dbus.Object
command *exec.Cmd
filename string
}
func New() OmxPlayer {
return OmxPlayer{}
}
func (o *OmxPlayer) IsPlaying() bool {
if o.command != nil {
return true
}
return false
}
func (o *OmxPlayer) FilePlaying() string {
if o.filename != "" {
return o.filename
}
return ""
}
func (o *OmxPlayer) PlayFile(filename string) error {
if o.IsPlaying() {
return errors.New("Error file is playing, please stop and try again")
}
o.command = exec.Command("omxplayer", "-b", "-o", "local", "--loop", filename)
o.filename = filename
err := o.command.Start()
if err != nil {
return err
}
time.Sleep(1000 * time.Millisecond)
env, err := ioutil.ReadFile("/tmp/omxplayerdbus")
if err != nil {
return err
}
os.Setenv("DBUS_SESSION_BUS_ADDRESS", string(env))
return nil
}
func (o *OmxPlayer) StopFile() error {
o.command.Process.Kill()
o.filename = ""
err := exec.Command("killall", "omxplayer.bin").Run()
return err
}
//@TODO DOESNT WORK ATM :(
func (o *OmxPlayer) PlayPause() error {
err := o.Method("org.mpris.MediaPlayer2.omxplayer.Player.PlayPause")
return err
}
func (o *OmxPlayer) Method(method string) error {
o.sdbus = connDbus()
c := o.sdbus.Call(method, 0)
return c.Err
}
func connDbus() *dbus.Object {
conn, err := dbus.SessionBusPrivate()
// couldnt connect to session bus
if err != nil {
panic(err)
}
return conn.Object("org.mpris.MediaPlayer2.omxplayer", "/org/mpris/MediaPlayer2")
}