Trait web_audio_api::media::MediaStream[][src]

pub trait MediaStream: Iterator<Item = Result<AudioBuffer, Box<dyn Error + Send>>> + Send + 'static { }
Expand description

Interface for media streaming.

This is a trait alias for an AudioBuffer Iterator, for example the OggVorbisDecoder or Microphone.

Below is an example showing how to play the stream directly in the audio context. However, this is typically not what you should do. The media stream will be polled on the render thread which will have catastrophic effects if the iterator blocks or for another reason takes too much time to yield a new sample frame.

The solution is to wrap the MediaStream inside a MediaElement. This will take care of buffering and timely delivery of audio to the render thread. It also allows for media playback controls (play/pause, offsets, loops, etc.)

Example

use web_audio_api::SampleRate;
use web_audio_api::context::{AudioContext, AsBaseAudioContext};
use web_audio_api::buffer::AudioBuffer;

// create a new buffer: 512 samples of silence
let silence = AudioBuffer::new(1, 512, SampleRate(44_100));

// create a sequence of this buffer
let sequence = std::iter::repeat(silence).take(5);

// the sequence should actually yield `Result<AudioBuffer, _>`s
let media = sequence.map(|b| Ok(b));

// media is now a proper `MediaStream` and can be used in the audio graph
let context = AudioContext::new(None);
let node = context.create_media_stream_source(media);

Implementors