Question

Is there any way for and audio player to output to two data sinks?

more specifically, i'm trying to stream an mp3 from the net using an audio player with the data source set as an uri, to a buffer AND the output mix. Right now i have the audio player's sink set to just an output mix, so i'm able to stream this file and play it when the prefetch says theres enough data, but i also want to put this streamed data into a buffer at the same time.

Was it helpful?

Solution

The answer is no, but there is a way around this.

I didn't realize that when you create an audio player with and output to a buffer queue, and a sink as an uri, that it plays to the buffer as fast as it gets the data.

So when i realized this is what was happening, i created another audio player which has a buffer queue as the source, and the output mix as the sink.

After everything is set up, I pause the first audio player (uriPlayer) which starts the "download" and streams the data to a buffer. when uriPlayer's prefetch says theres enough data, i check the "duration" of the uriPlayer to find out how long the song is, which i then create a buffer that is big enough to hold the pcm data from the uriPlayer based on the duration and sample rate (and output channels, which is 2 for stereo, and 1 for mono).

I enqueue this buffer into the uriPlayers sink buffer queue, then set the uriPlayers state to Play, so it starts loading all the data into the buffer in its buffer queue.

I then enqueue this same buffer into the second players (musicPlayer) source buffer, and set its state to Play also, which starts playing the buffer in its buffer queue.

as long as the uriPlayer can download the contents of the music file faster than the music player plays from the buffer, just by calling getPosition from the uriPlayer to find out where in the song its currently streaming and if its "ahead" of the position musicPlayer is currently playing, everything works perfectly! if the download slows down or is too slow, just pause musicPlayer if it gets ahead of uriPlayer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top