Home

Play An Audio File To Loopback With Rust

rust
```cargo
[dependencies]
cpal = "0.15"
rodio = "0.17"
```

use std::fs::File;
use std::io::BufReader;
use rodio::{Decoder, OutputStream, Sink};
use cpal::traits::HostTrait;
use cpal::traits::DeviceTrait;

fn main() { 

  let host = cpal::default_host();
  if let Ok(output_devices) = host.input_devices() {
    output_devices.for_each(|d| {
      if let Ok(name) = d.name() {
        if name == "LoopbackTesting".to_string() {
          println!("{}", name);
          let (_stream, stream_handle) = OutputStream::try_from_device(&d).unwrap();
          let file = BufReader::new(File::open("audio/hydrate.mp3").unwrap());
          let sink = Sink::try_new(&stream_handle).unwrap();
          let source = Decoder::new(file).unwrap();
          sink.append(source);
          sink.sleep_until_end();
        }
      }
    });
  }

}
results start

This plays a sound from a rust app to a sounds interface from Mac's loopback app. I use this to send stuff to OBS when I'm streaming

This works, but is really just a prototype to play with to prove the concept

~ fin ~

References