pub struct TrackHandle { /* private fields */ }Expand description
Controls a track that Call::play or
Call::enqueue gave the call: pause it, play it again, stop it,
change its volume, loop it, seek in it, read its state, follow its events.
A cheap-clone handle: clones control the same track. The controls only ask the call to
act, so they return at once (seek waits for it); they fail with
TrackError::Ended once the track has ended (played to its end, stopped, replaced,
skipped, its input failed, or its call ended). Dropping every handle does not stop the
track.
discord.js counterpart: AudioPlayer#pause, unpause and stop, and
AudioResource#volume of @discordjs/voice (a player plays one resource); songbird:
TrackHandle.
§Examples
use std::time::Duration;
use djs_voice::{Input, Loop};
let track = call.play(Input::ffmpeg("song.mp3"));
track.set_volume(0.5)?;
track.set_loop(Loop::Times(2))?;
track.seek(Duration::from_secs(30)).await?;
track.pause()?;
track.play()?;
println!("at {:?}", track.info().position);
track.stop()?;
assert!(track.play().is_err(), "a stopped track has ended");Implementations§
Source§impl TrackHandle
impl TrackHandle
Sourcepub fn play(&self) -> Result<(), TrackError>
pub fn play(&self) -> Result<(), TrackError>
Plays the track again after pause, from where it paused; does
nothing to a track that is playing. A waiting track that was paused starts playing
when its turn comes.
discord.js counterpart: AudioPlayer#unpause.
§Errors
TrackError::Ended when the track has ended.
Sourcepub fn pause(&self) -> Result<(), TrackError>
pub fn pause(&self) -> Result<(), TrackError>
Pauses the track: it stops sending audio (after a few frames of silence, as Discord
asks) and keeps its place; play goes on from there. A waiting track
starts paused when its turn comes (D-361).
discord.js counterpart: AudioPlayer#pause.
§Errors
TrackError::Ended when the track has ended.
Sourcepub fn stop(&self) -> Result<(), TrackError>
pub fn stop(&self) -> Result<(), TrackError>
Stops the track for good (TrackEnd::Stopped): its input is no longer read. The
call’s queue goes on with the next track; a waiting track is removed from the queue.
discord.js counterpart: AudioPlayer#stop.
§Errors
TrackError::Ended when the track has already ended.
Sourcepub fn set_volume(&self, volume: f32) -> Result<(), TrackError>
pub fn set_volume(&self, volume: f32) -> Result<(), TrackError>
Sets the track’s volume (D-353): linear, 1.0 the original, 0.5 half as loud, 0.0
silent, up to 2.0 (louder, with a soft clip against distortion); values outside
0.0..=2.0 are clamped, NaN counts as 0.0. Heard from the next frame.
At 1.0 an Ogg/Opus track is sent as it is; at any other volume it is decoded and encoded again, which costs some CPU.
discord.js counterpart: AudioResource#volume.setVolume (with inlineVolume);
songbird: TrackHandle::set_volume.
§Errors
TrackError::Ended when the track has ended.
Sourcepub fn set_loop(&self, looping: Loop) -> Result<(), TrackError>
pub fn set_loop(&self, looping: Loop) -> Result<(), TrackError>
Sets whether the track plays again when it reaches its end (D-358): never, forever,
or a number of times more. Each repeat sends TrackEvent::Looped;
TrackInfo::looping shows the repeats left.
discord.js counterpart: none (a bot plays the resource again); songbird:
TrackHandle::enable_loop, disable_loop and loop_for.
§Errors
TrackError::Ended when the track has ended, TrackError::Unsupported when its
input is a reader, which cannot be opened again (D-360).
Sourcepub async fn seek(&self, position: Duration) -> Result<Duration, TrackError>
pub async fn seek(&self, position: Duration) -> Result<Duration, TrackError>
Plays the track from position (D-359); returns once it plays from there, with the
position it actually plays from: the start of the Opus packet holding position for
an Ogg/Opus file, position itself with FFmpeg. A waiting track will start there
(the result comes at once). Seeking past the end ends the track
(TrackEnd::Finished), unless it loops.
The file is opened again (its packets skipped up to position, without decoding), or
FFmpeg run again with -ss.
discord.js counterpart: none (a resource is played again with FFmpeg’s -ss);
songbird: TrackHandle::seek.
§Errors
TrackError::Ended when the track has ended (or ends while seeking: past its end,
or its input failed), TrackError::Unsupported when its input is a reader, which
cannot be opened again (D-360).
Sourcepub fn info(&self) -> TrackInfo
pub fn info(&self) -> TrackInfo
The track’s state now: playing, paused, waiting or ended, where it is, its volume and loop (D-356). Read at once, without asking the call.
discord.js counterpart: AudioPlayer#state and AudioResource#playbackDuration;
songbird: TrackHandle::get_info.
Sourcepub fn events(&self) -> TrackEvents
pub fn events(&self) -> TrackEvents
Returns a stream of the track’s events (D-355): it plays, pauses, loops, and ends,
with why. Each stream gets every event from its creation on, and ends after
TrackEvent::Ended; a stream of a track that already ended ends at once.
discord.js counterpart: AudioPlayer#on('stateChange'); songbird:
TrackHandle::add_event.
§Examples
use djs_voice::{Input, TrackEnd, TrackEvent};
let track = call.enqueue(Input::ffmpeg("song.mp3"));
let mut events = track.events();
while let Some(event) = events.next().await {
match event {
TrackEvent::Playing => println!("now playing"),
TrackEvent::Ended(TrackEnd::Failed(error)) => println!("failed: {error}"),
TrackEvent::Ended(reason) => println!("ended: {reason}"),
_ => {}
}
}Trait Implementations§
Source§impl Clone for TrackHandle
impl Clone for TrackHandle
Source§fn clone(&self) -> TrackHandle
fn clone(&self) -> TrackHandle
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TrackHandle
impl Debug for TrackHandle
impl Eq for TrackHandle
Auto Trait Implementations§
impl Freeze for TrackHandle
impl RefUnwindSafe for TrackHandle
impl Send for TrackHandle
impl Sync for TrackHandle
impl Unpin for TrackHandle
impl UnsafeUnpin for TrackHandle
impl UnwindSafe for TrackHandle
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> Classify for T
impl<T> Classify for T
type Classified = T
fn classify(self) -> T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Declassify for T
impl<T> Declassify for T
type Declassified = T
fn declassify(self) -> T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more