jackz revised this gist 1 year ago. Go to revision
No changes
Jackz revised this gist 4 years ago. Go to revision
1 file changed, 30 insertions
play_audio_v01.rs(file created)
| @@ -0,0 +1,30 @@ | |||
| 1 | + | use mlua::prelude::*; | |
| 2 | + | use std::fs::File; | |
| 3 | + | use std::io::BufReader; | |
| 4 | + | use rodio::{Decoder, OutputStream, Sink}; | |
| 5 | + | use std::time::Duration; | |
| 6 | + | use futures_timer::Delay; | |
| 7 | + | ||
| 8 | + | async fn play_local_file(lua: &Lua, (filepath, volume, duration): (String, f32, u64)) -> LuaResult<()> { | |
| 9 | + | let (_stream, stream_handle) = OutputStream::try_default().unwrap(); | |
| 10 | + | let sink = Sink::try_new(&stream_handle).unwrap(); | |
| 11 | + | sink.set_volume(volume); | |
| 12 | + | let file = BufReader::new(File::open(filepath).unwrap()); | |
| 13 | + | let source = Decoder::new(file).unwrap(); | |
| 14 | + | sink.append(source); | |
| 15 | + | Delay::new(Duration::from_millis(duration)).await; | |
| 16 | + | Ok(()) | |
| 17 | + | } | |
| 18 | + | ||
| 19 | + | #[tokio::main] | |
| 20 | + | #[mlua::lua_module] | |
| 21 | + | async fn lua_audio(lua: &Lua) -> LuaResult<LuaTable> { | |
| 22 | + | let exports = lua.create_table()?; | |
| 23 | + | exports.set("play_from_file", lua.create_async_function(play_local_file)?)?; | |
| 24 | + | Ok(exports) | |
| 25 | + | } | |
| 26 | + | ||
| 27 | + | #[mlua::lua_module] | |
| 28 | + | fn rust_module_error(_: &Lua) -> LuaResult<LuaTable> { | |
| 29 | + | Err("custom module error".to_lua_err()) | |
| 30 | + | } | |
Newer
Older