forked from Elara6331/infinitime
Initial Commit
This commit is contained in:
16
pkg/player/pactl.go
Normal file
16
pkg/player/pactl.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// VolUp uses pactl to increase the volume of the default sink
|
||||
func VolUp(percent uint) error {
|
||||
return exec.Command("pactl", "set-sink-volume", "@DEFAULT_SINK@", fmt.Sprintf("+%d%%", percent)).Run()
|
||||
}
|
||||
|
||||
// VolDown uses pactl to decrease the volume of the default sink
|
||||
func VolDown(percent uint) error {
|
||||
return exec.Command("pactl", "set-sink-volume", "@DEFAULT_SINK@", fmt.Sprintf("-%d%%", percent)).Run()
|
||||
}
|
||||
91
pkg/player/playerctl.go
Normal file
91
pkg/player/playerctl.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Play uses playerctl to play media
|
||||
func Play() error {
|
||||
return exec.Command("playerctl", "play").Run()
|
||||
}
|
||||
|
||||
// Pause uses playerctl to pause media
|
||||
func Pause() error {
|
||||
return exec.Command("playerctl", "pause").Run()
|
||||
}
|
||||
|
||||
// Next uses playerctl to skip to next media
|
||||
func Next() error {
|
||||
return exec.Command("playerctl", "next").Run()
|
||||
}
|
||||
|
||||
// Prev uses playerctl to skip to previous media
|
||||
func Prev() error {
|
||||
return exec.Command("playerctl", "previous").Run()
|
||||
}
|
||||
|
||||
// Metadata uses playerctl to detect music metadata changes
|
||||
func Metadata(key string, onChange func(string)) error {
|
||||
// Execute playerctl command with key and follow flag
|
||||
cmd := exec.Command("playerctl", "metadata", key, "-F")
|
||||
// Get stdout pipe
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
// Read line from command stdout
|
||||
line, _, err := bufio.NewReader(stdout).ReadLine()
|
||||
if err == io.EOF {
|
||||
continue
|
||||
}
|
||||
// Convert line to string
|
||||
data := string(line)
|
||||
// If key unknown, return suitable default
|
||||
if data == "No player could handle this command" || data == "" {
|
||||
data = "Unknown " + strings.Title(key)
|
||||
}
|
||||
// Run the onChange callback
|
||||
onChange(data)
|
||||
}
|
||||
}()
|
||||
// Start command asynchronously
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Status(onChange func(bool)) error {
|
||||
// Execute playerctl status with follow flag
|
||||
cmd := exec.Command("playerctl", "status", "-F")
|
||||
// Get stdout pipe
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
// Read line from command stdout
|
||||
line, _, err := bufio.NewReader(stdout).ReadLine()
|
||||
if err == io.EOF {
|
||||
continue
|
||||
}
|
||||
// Convert line to string
|
||||
data := string(line)
|
||||
// Run the onChange callback
|
||||
onChange(data == "Playing")
|
||||
}
|
||||
}()
|
||||
// Start command asynchronously
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user