streaming/streaming.rs
1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use fyrox_resource::io::FsResourceIo;
22use fyrox_sound::buffer::SoundBufferResourceExtension;
23use fyrox_sound::{
24 buffer::{DataSource, SoundBufferResource},
25 context::SoundContext,
26 engine::SoundEngine,
27 futures::executor::block_on,
28 pool::Handle,
29 source::{SoundSource, SoundSourceBuilder, Status},
30};
31use std::{thread, time::Duration};
32
33fn main() {
34 // Initialize sound engine with default output device.
35 let engine = SoundEngine::new().unwrap();
36
37 // Initialize new sound context.
38 let context = SoundContext::new();
39
40 engine.state().add_context(context.clone());
41
42 // Load sound buffer.
43 let waterfall_buffer = SoundBufferResource::new_streaming(
44 block_on(DataSource::from_file(
45 "examples/data/waterfall.ogg",
46 // Load from the default resource io (File system)
47 &FsResourceIo,
48 ))
49 .unwrap(),
50 )
51 .unwrap();
52
53 // Create flat source (without spatial effects) using that buffer.
54 let source = SoundSourceBuilder::new()
55 .with_buffer(waterfall_buffer)
56 .with_status(Status::Playing)
57 .with_looping(true)
58 .build()
59 .unwrap();
60
61 // Each sound sound must be added to context, context takes ownership on source
62 // and returns pool handle to it by which it can be accessed later on if needed.
63 let _source_handle: Handle<SoundSource> = context.state().add_source(source);
64
65 thread::sleep(Duration::from_secs(30))
66}