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 { 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 { Err("custom module error".to_lua_err()) }