Skip to main content

TrackHandle

Struct TrackHandle 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

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).

Source

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.

Source

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

Source§

fn clone(&self) -> TrackHandle

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TrackHandle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Eq for TrackHandle

Source§

impl PartialEq for TrackHandle

Source§

fn eq(&self, other: &TrackHandle) -> bool

Whether both handles control the same track.

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Classify for T

§

type Classified = T

§

fn classify(self) -> T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> Declassify for T

§

type Declassified = T

§

fn declassify(self) -> T

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more