Skip to main content

streaming/
streaming.rs

1use rg3d_sound::engine::SoundEngine;
2use rg3d_sound::futures::executor::block_on;
3use rg3d_sound::{
4    buffer::{DataSource, SoundBufferResource},
5    context::SoundContext,
6    pool::Handle,
7    source::{generic::GenericSourceBuilder, SoundSource, Status},
8};
9use std::{thread, time::Duration};
10
11fn main() {
12    // Initialize sound engine with default output device.
13    let engine = SoundEngine::new();
14
15    // Initialize new sound context.
16    let context = SoundContext::new();
17
18    engine.lock().unwrap().add_context(context.clone());
19
20    // Load sound buffer.
21    let waterfall_buffer = SoundBufferResource::new_streaming(
22        block_on(DataSource::from_file("examples/data/waterfall.ogg")).unwrap(),
23    )
24    .unwrap();
25
26    // Create flat source (without spatial effects) using that buffer.
27    let source = GenericSourceBuilder::new()
28        .with_buffer(waterfall_buffer)
29        .with_status(Status::Playing)
30        .with_looping(true)
31        .build_source()
32        .unwrap();
33
34    // Each sound sound must be added to context, context takes ownership on source
35    // and returns pool handle to it by which it can be accessed later on if needed.
36    let _source_handle: Handle<SoundSource> = context.state().add_source(source);
37
38    thread::sleep(Duration::from_secs(30))
39}