Skip to content

Commit

Permalink
Next and Previous tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
hilli committed Jan 13, 2024
1 parent 92663bc commit 83e6c96
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cmd/kefw2/cmd/next_track.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"fmt"
"os"

"github.com/hilli/go-kef-w2/kefw2"
"github.com/spf13/cobra"
)

// muteCmd toggles the mute state of the speakers
var nextTrackCmd = &cobra.Command{
Use: "next",
Short: "Play next track when on WiFi source",
Long: `Play next track when on WiFi source`,
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
currentSource, err := currentSpeaker.Source()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if currentSource != kefw2.SourceWiFi {
fmt.Println("Not on WiFi source, not resuming playback")
os.Exit(0)
}
err = currentSpeaker.NextTrack()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
rootCmd.AddCommand(nextTrackCmd)
}
38 changes: 38 additions & 0 deletions cmd/kefw2/cmd/prevous_track.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"fmt"
"os"

"github.com/hilli/go-kef-w2/kefw2"
"github.com/spf13/cobra"
)

// muteCmd toggles the mute state of the speakers
var previousTrackCmd = &cobra.Command{
Use: "previous",
Aliases: []string{"prev"},
Short: "Play previous track when on WiFi source",
Long: `Play previous track when on WiFi source`,
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
currentSource, err := currentSpeaker.Source()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if currentSource != kefw2.SourceWiFi {
fmt.Println("Not on WiFi source, not resuming playback")
os.Exit(0)
}
err = currentSpeaker.PreviousTrack()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
rootCmd.AddCommand(previousTrackCmd)
}
10 changes: 10 additions & 0 deletions kefw2/kefw2.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,13 @@ func (s *KEFSpeaker) IsPlaying() (bool, error) {
}
return pd.State == "playing", nil
}

// NextTrack works only if the speaker is playing in wifi mode
func (s *KEFSpeaker) NextTrack() error {
return s.setActivate("player:player/control", "control", "next")
}

// PreviousTrack works only if the speaker is playing in wifi mode
func (s *KEFSpeaker) PreviousTrack() error {
return s.setActivate("player:player/control", "control", "previous")
}

0 comments on commit 83e6c96

Please sign in to comment.