fmod/studio/event_instance/playback.rs
1// Copyright (c) 2024 Melody Madeline 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};
10use crate::{FmodResultExt, Result};
11
12impl EventInstance {
13 /// Starts playback.
14 ///
15 ///If the instance was already playing then calling this function will restart the event.
16 ///
17 /// Generally it is a best practice to call [`EventInstance::release`] on event instances immediately after starting them,
18 /// unless you want to play the event instance multiple times or explicitly stop it and start it again later.
19 pub fn start(&self) -> Result<()> {
20 unsafe { FMOD_Studio_EventInstance_Start(self.inner.as_ptr()).to_result() }
21 }
22
23 /// Stops playback.
24 pub fn stop(&self, mode: StopMode) -> Result<()> {
25 unsafe { FMOD_Studio_EventInstance_Stop(self.inner.as_ptr(), mode.into()).to_result() }
26 }
27
28 /// Retrieves the playback state.
29 ///
30 /// You can poll this function to track the playback state of an event instance.
31 ///
32 /// If the instance is invalid, then the state will be set to [`PlaybackState::Stopped`].
33 pub fn get_playback_state(&self) -> Result<PlaybackState> {
34 let mut state = 0;
35 unsafe {
36 FMOD_Studio_EventInstance_GetPlaybackState(self.inner.as_ptr(), &raw mut state)
37 .to_result()?;
38 }
39 let state = state.try_into()?;
40 Ok(state)
41 }
42
43 /// Sets the pause state.
44 ///
45 /// This function allows pausing/unpausing of an event instance.
46 pub fn set_paused(&self, paused: bool) -> Result<()> {
47 unsafe {
48 FMOD_Studio_EventInstance_SetPaused(self.inner.as_ptr(), paused.into()).to_result()
49 }
50 }
51
52 /// Retrieves the paused state.
53 pub fn get_paused(&self) -> Result<bool> {
54 let mut paused = FMOD_BOOL::FALSE;
55 unsafe {
56 FMOD_Studio_EventInstance_GetPaused(self.inner.as_ptr(), &raw mut paused)
57 .to_result()?;
58 }
59 Ok(paused.into())
60 }
61
62 /// Allow an event to continue past a sustain point.
63 ///
64 /// 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.
65 ///
66 /// This function returns [`FMOD_RESULT::FMOD_ERR_EVENT_NOTFOUND`] if the event has no sustain points.
67 pub fn key_off(&self) -> Result<()> {
68 unsafe { FMOD_Studio_EventInstance_KeyOff(self.inner.as_ptr()).to_result() }
69 }
70}