1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::io::BufReader;
use mlua::Lua;
use crate::create_body;
#[doc(hidden)]
pub fn init(lua: &Lua) -> mlua::Result<mlua::Table> {
create_body!(lua,
"play_audio_blocking" => lua.create_function(play_audio_blocking)?,
"play_audio" => lua.create_function(play_audio)?
)
}
pub fn play_audio_blocking(_: &Lua, fname: String) -> mlua::Result<()> {
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();
let file = std::fs::File::open(fname).unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
sink.sleep_until_end();
Ok(())
}
pub fn play_audio(_: &Lua, fname: String) -> mlua::Result<()> {
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();
let file = std::fs::File::open(fname).unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
Ok(())
}