Skip to content

Commit 59389a0

Browse files
yteraokavishen
authored andcommitted
Add volume subcommand
The volume subcommand can get and change volumes.
1 parent d9165c1 commit 59389a0

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Available Commands:
6565
tts text-to-speech
6666
ui Run the UI
6767
unpause Unpause the currently playing media on the chromecast
68+
volume Get or set volume
6869
watch Watch all events sent from a chromecast device
6970
7071
Flags:
@@ -167,6 +168,12 @@ $ go-chromecast rewind 30
167168
# Go forward in the currently playing media by x seconds.
168169
$ go-chromecast seek 30
169170
171+
# Get the current volume level
172+
$ go-chromecast volume
173+
174+
# Set the volume level
175+
$ go-chromecast volume 0.55
176+
170177
# View what a cast device is sending out.
171178
$ go-chromecast watch
172179

cmd/volume.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright © 2018 Jonathan Pentecost <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
"strconv"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// volumeCmd represents the volume command
25+
var volumeCmd = &cobra.Command{
26+
Use: "volume [<0.00 - 1.00>]",
27+
Short: "Get or set volume",
28+
Long: "Get or set volume (float in range from 0 to 1)",
29+
Run: func(cmd *cobra.Command, args []string) {
30+
app, err := castApplication(cmd, args)
31+
if err != nil {
32+
fmt.Printf("unable to get cast application: %v\n", err)
33+
return
34+
}
35+
36+
if len(args) == 1 && args[0] != "" {
37+
newVolume, err := strconv.ParseFloat(args[0], 32)
38+
if err != nil {
39+
fmt.Printf("invalid volume: %v\n", err)
40+
return
41+
}
42+
if err = app.SetVolume(float32(newVolume)); err != nil {
43+
fmt.Printf("failed to set volume: %v\n", err)
44+
return
45+
}
46+
}
47+
48+
if err = app.Update(); err != nil {
49+
fmt.Printf("unable to update cast info: %v\n", err)
50+
return
51+
}
52+
_, _, castVolume := app.Status()
53+
54+
fmt.Printf("%0.2f\n", castVolume.Level)
55+
56+
return
57+
},
58+
}
59+
60+
func init() {
61+
rootCmd.AddCommand(volumeCmd)
62+
}

0 commit comments

Comments
 (0)