Skip to content

Instantly share code, notes, and snippets.

@Jackzmc
Created August 17, 2021 19:58
Show Gist options
  • Save Jackzmc/8a1d506dd06d21ded17daaaf645153ba to your computer and use it in GitHub Desktop.
Save Jackzmc/8a1d506dd06d21ded17daaaf645153ba to your computer and use it in GitHub Desktop.
use mlua::prelude::*;
use std::fs::File;
use std::io::BufReader;
use rodio::{Decoder, OutputStream, Sink};
use std::time::Duration;
use futures_timer::Delay;
async fn play_local_file(lua: &Lua, (filepath, volume, duration): (String, f32, u64)) -> LuaResult<()> {
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
sink.set_volume(volume);
let file = BufReader::new(File::open(filepath).unwrap());
let source = Decoder::new(file).unwrap();
sink.append(source);
Delay::new(Duration::from_millis(duration)).await;
Ok(())
}
#[tokio::main]
#[mlua::lua_module]
async fn lua_audio(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
exports.set("play_from_file", lua.create_async_function(play_local_file)?)?;
Ok(exports)
}
#[mlua::lua_module]
fn rust_module_error(_: &Lua) -> LuaResult<LuaTable> {
Err("custom module error".to_lua_err())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment