Created
August 17, 2021 19:58
-
-
Save Jackzmc/8a1d506dd06d21ded17daaaf645153ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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