Problem
I get an error when I run the following code.
// In Rust
pub fn play_music() {
let host = cpal::default_host();
let mut devices = host.output_devices().unwrap();
let device = devices
.find(|d| d.name().unwrap().contains("CABLE"))
.unwrap();
let (_stream, stream_handle) = rodio::OutputStream::try_from_device(&device).unwrap();
let file =
File::open("(music file path)").unwrap();
let sink = stream_handle.play_once(file).unwrap();
sink.play();
loop {}
}
fn play_ffi(mut cx: FunctionContext) -> JsResult<JsUndefined> {
play_music();
// std::thread::spawn(move || play_music());
Ok(cx.undefined())
}
// In electron main process.
const native = require('../native');
ipcMain.on('play', (event, arg) => {
native.play_ffi(arg);
});
Goal
The execution statement that causes the error is let mut devices = host.output_devices().unwrap();
.
It turns out that
if you comment out the first line of the function play_ffi
and execute the second line (std::thread::spawn(move || play_music());
), it will execute normally.
However, I don't want to create a new thread, so I'm looking for a way to do it without creating a thread.
Development Environment
node.js:
- "electron": "^11.2.2"
- "neon-cli": "^0.7.1"
Rust:
- "neon" = "0.7.1"
- "cpal" = "0.13.1"
- "rodio" = "0.13.0"