0

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);
});

enter image description here

enter image description here

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"
vallentin
  • 23,478
  • 6
  • 59
  • 81
Romira
  • 1
  • It looks like the error message (in Japanese), is "cannot change thread mode after it is set". It looks like this might be caused by not recompiling the binary for electron with `electron-rebuild`? I'm just spitballing, I don't know electron very well but though others may appreciate knowing the exact error message. – Linear Feb 13 '21 at 00:29
  • It has been done. – Romira Feb 13 '21 at 12:09

0 Answers0