Skip to main content

play_sound/
play_sound.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    pool::Handle,
28    source::{SoundSource, SoundSourceBuilder, Status},
29};
30use std::{thread, time::Duration};
31
32fn main() {
33    // Initialize sound engine with default output device.
34    let engine = SoundEngine::new().unwrap();
35
36    // Create new context.
37    let context = SoundContext::new();
38
39    // Register context in the engine.
40    engine.state().add_context(context.clone());
41
42    // Load sound buffer.
43    let door_open_buffer = SoundBufferResource::new_generic(
44        fyrox_sound::futures::executor::block_on(DataSource::from_file(
45            "examples/data/door_open.wav",
46            // Load from the default resource io (File system)
47            &FsResourceIo,
48        ))
49        .unwrap(),
50    )
51    .unwrap();
52
53    // Create generic source (without spatial effects) using that buffer.
54    let source = SoundSourceBuilder::new()
55        .with_buffer(door_open_buffer)
56        .with_status(Status::Playing)
57        // Ensure that no spatial effects will be applied.
58        .with_spatial_blend_factor(0.0)
59        .build()
60        .unwrap();
61
62    // Each sound sound must be added to context, context takes ownership on source
63    // and returns pool handle to it by which it can be accessed later on if needed.
64    let _source_handle: Handle<SoundSource> = context.state().add_source(source);
65
66    // Wait until sound will play completely.
67    thread::sleep(Duration::from_secs(3));
68}