fmod/studio/event_instance/
playback.rs

1// Copyright (c) 2024 Lily Lyons
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use fmod_sys::*;
8
9use crate::studio::{EventInstance, PlaybackState, StopMode};
10
11impl EventInstance {
12    /// Starts playback.
13    ///
14    ///If the instance was already playing then calling this function will restart the event.
15    ///
16    /// Generally it is a best practice to call [`EventInstance::release`] on event instances immediately after starting them,
17    /// unless you want to play the event instance multiple times or explicitly stop it and start it again later.
18    pub fn start(&self) -> Result<()> {
19        unsafe { FMOD_Studio_EventInstance_Start(self.inner).to_result() }
20    }
21
22    /// Stops playback.
23    pub fn stop(&self, mode: StopMode) -> Result<()> {
24        unsafe { FMOD_Studio_EventInstance_Stop(self.inner, mode.into()).to_result() }
25    }
26
27    /// Retrieves the playback state.
28    ///
29    /// You can poll this function to track the playback state of an event instance.
30    ///
31    /// If the instance is invalid, then the state will be set to [`PlaybackState::Stopped`].
32    pub fn get_playback_state(&self) -> Result<PlaybackState> {
33        let mut state = 0;
34        unsafe {
35            FMOD_Studio_EventInstance_GetPlaybackState(self.inner, &mut state).to_result()?;
36        }
37        let state = state.try_into()?;
38        Ok(state)
39    }
40
41    /// Sets the pause state.
42    ///
43    /// This function allows pausing/unpausing of an event instance.
44    pub fn set_paused(&self, paused: bool) -> Result<()> {
45        unsafe { FMOD_Studio_EventInstance_SetPaused(self.inner, paused.into()).to_result() }
46    }
47
48    /// Retrieves the paused state.
49    pub fn get_paused(&self) -> Result<bool> {
50        let mut paused = FMOD_BOOL::FALSE;
51        unsafe {
52            FMOD_Studio_EventInstance_GetPaused(self.inner, &mut paused).to_result()?;
53        }
54        Ok(paused.into())
55    }
56
57    /// Allow an event to continue past a sustain point.
58    ///
59    /// Multiple sustain points may be bypassed ahead of time and the key off count will be decremented each time the timeline cursor passes a sustain point.
60    ///
61    /// This function returns [`FMOD_RESULT::FMOD_ERR_EVENT_NOTFOUND`] if the event has no sustain points.
62    pub fn key_off(&self) -> Result<()> {
63        unsafe { FMOD_Studio_EventInstance_KeyOff(self.inner).to_result() }
64    }
65}