Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] play without blocking main thread #310

Closed
YXL76 opened this issue Aug 12, 2020 · 0 comments
Closed

[Question] play without blocking main thread #310

YXL76 opened this issue Aug 12, 2020 · 0 comments

Comments

@YXL76
Copy link
Contributor

YXL76 commented Aug 12, 2020

Thanks to neon, I can use Rodio in nodejs.
When using 0.11.0, it can play audio in background and do not block the process.

use neon::prelude::*;
use std::{fs::File, io::BufReader};

fn load(mut cx: FunctionContext) -> JsResult<JsUndefined> {
    let url = cx.argument::<JsString>(0)?.value();

    let device = rodio::default_output_device().unwrap();
    let sink = rodio::Sink::new(&device);

    let file = File::open(url).unwrap();
    sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());

    Ok(cx.undefined())
}

register_module!(mut cx, { cx.export_function("load", load) });

But when using git version, there is no sound unless add sleep_until_end. However, the main thread is blocked.

use neon::prelude::*;
use std::{fs::File, io::BufReader};

fn load(mut cx: FunctionContext) -> JsResult<JsUndefined> {
    let url = cx.argument::<JsString>(0)?.value();

    let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
    let sink = rodio::Sink::try_new(&handle).unwrap();

    let file = File::open(url).unwrap();
    sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());

    // play sound only if uncomment the next line
    // sink.sleep_until_end();

    Ok(cx.undefined())
}

register_module!(mut cx, { cx.export_function("load", load) });

Therefore, my question is whether there is a way to play sound without blocking main thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant