change_device_vol/
change_device_vol.rs

1use std::io::{self, Write};
2
3use pulsectl::controllers::DeviceControl;
4use pulsectl::controllers::SinkController;
5
6fn main() {
7    // create handler that calls functions on playback devices and apps
8    let mut handler = SinkController::create().unwrap();
9    let devices = handler
10        .list_devices()
11        .expect("Could not get list of playback devices");
12
13    println!("Playback Devices: ");
14    for device in &devices {
15        println!(
16            "[Index: {}] {}, [Volume: {}]",
17            device.index,
18            device.description.as_ref().unwrap(),
19            device.volume
20        );
21    }
22
23    let mut input = String::new();
24
25    print!("Select an index: ");
26    io::stdout().flush().expect("Failed to flush stdout");
27
28    io::stdin()
29        .read_line(&mut input)
30        .expect("Unable to read user input");
31
32    println!("Increasing volume by 5%...");
33
34    let device_index = input.trim().parse().expect("Invalid number");
35    handler.increase_device_volume_by_percent(device_index, 0.05);
36
37    println!(
38        "Volume set to [Volume: {}]",
39        handler
40            .get_device_by_index(device_index)
41            .expect("Failed to get device by index")
42            .volume
43    )
44}