implement play/pause and volume control

This commit is contained in:
Max Bradbury 2020-09-23 11:02:08 +01:00
parent 6235d564e4
commit 40c100e65b
1 changed files with 16 additions and 2 deletions

View File

@ -1,7 +1,7 @@
use gio::prelude::*; use gio::prelude::*;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::Orientation; use gtk::Orientation;
use rodio::Source; use rodio::Sink;
use std::env::args; use std::env::args;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
@ -44,7 +44,9 @@ fn build_ui(application: &gtk::Application) {
let source = rodio::Decoder::new(BufReader::new(file)) let source = rodio::Decoder::new(BufReader::new(file))
.expect("Couldn't decode audio"); .expect("Couldn't decode audio");
rodio::play_raw(&device, source.convert_samples()); let sink = Sink::new(&device);
sink.append(source);
sink.pause();
let row = gtk::Box::new(Orientation::Horizontal, SPACING); let row = gtk::Box::new(Orientation::Horizontal, SPACING);
row.set_homogeneous(true); row.set_homogeneous(true);
@ -68,6 +70,18 @@ fn build_ui(application: &gtk::Application) {
slider.set_draw_value(false); slider.set_draw_value(false);
slider.connect_value_changed(move |scale| {
let volume = scale.get_value();
println!("volume: {}", volume);
if volume == 0. {
sink.pause();
} else {
sink.play();
sink.set_volume(volume as f32);
}
});
row.add(&slider); row.add(&slider);
vertical.add(&row); vertical.add(&row);