Skip to content

Commit

Permalink
add v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
amamic1803 committed Jan 28, 2023
1 parent 5d7a88c commit 4add6e4
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
125 changes: 125 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "Win-Set-AudioOUT"
version = "1.0.0"
edition = "2021"
build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tempfile = "3.3.0"

[build-dependencies]
winres = "0.1.12"
7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
if cfg!(target_os = "windows") {
let mut res = winres::WindowsResource::new();
res.set_icon("data\\audio-icon.ico");
res.compile().unwrap();
}
}
69 changes: 69 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#![allow(non_snake_case)]
#![windows_subsystem = "windows"]

use tempfile::tempdir;
use std::fs::File;
use std::io::prelude::*;
use std::io::Write;
use std::process::Command;
use std::{thread, time};


fn main() {
let bundled_soundvolumeview = include_bytes!("../data/soundvolumeview-x64/SoundVolumeView.exe");

// Create a directory inside of `std::env::temp_dir()`
let dir = tempdir().unwrap();
let soundvolumeview_path = dir.path().join("SoundVolumeView.exe");
let audioout_path = dir.path().join("audioout.txt");
let mut file = File::create(&soundvolumeview_path).unwrap();
file.write_all(bundled_soundvolumeview).unwrap();
drop(file);

change_audio_device(soundvolumeview_path.to_str().unwrap(), audioout_path.to_str().unwrap());

drop(dir);
}

fn change_audio_device(soundvolumeview_path: &str, audioout_path: &str) {
Command::new(soundvolumeview_path)
.arg("/RunAsAdmin")
.arg("/scomma")
.arg(audioout_path)
.status().unwrap();

thread::sleep(time::Duration::from_millis(500));

let mut audioout_file = File::open(audioout_path).unwrap();
let mut audioout_contents = String::new();
audioout_file.read_to_string(&mut audioout_contents).unwrap();

let mut default_audio_device = "";
for line in audioout_contents.trim().split('\n') {
if line.contains("Render,Render") {
default_audio_device = line.split(',').collect::<Vec<&str>>()[0];
}
}

match default_audio_device {
"Headset" => {
Command::new(soundvolumeview_path)
.arg("/RunAsAdmin")
.arg("/SetDefault")
.arg("Speakers")
.arg("all")
.status().unwrap();
},
"Speakers" => {
Command::new(soundvolumeview_path)
.arg("/RunAsAdmin")
.arg("/SetDefault")
.arg("Headset")
.arg("all")
.status().unwrap();
},
_ => ()
}

thread::sleep(time::Duration::from_millis(500));
}

0 comments on commit 4add6e4

Please sign in to comment.