Sender: how to know when all the data has been sent, and how to restart? #2012
-
My code needs to be able to connect to a BT speaker, and when triggered, send audio data from program memory. I have this working like many of the examples, i.e. basically everything is done in setup and loop has no "interesting" code. I imagine I would want to keep the volume setting and connection code in setup, and perhaps the set_data_callback. Is there something that tells me in the callback function that all the data has been read/sent? I am using a MemoryStream, a MP3DecoderHelix, and an EncodedAudioStream object. Is there something I receive in the callback function's arguments that would tell me I'm done, or should I keep track of the count of bytes that have been sent? Once all the data has been sent, do I keep getting called at the callback function? How do I reset those objects to start sending the same audio data? Essentially, I would like to set the stage for playback during setup. But then, when something triggers a condition, start playing the audio data. When all the data has been sent, note the time. After some "idle" time has elapsed, watch for the next trigger, and start sending the same audio data again, from the beginning. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The connections stays open as long as you return data: It is your choice if you want to return any pcm data or just silence data (an array with 0). You can easily track if the full MemoryStream has been played by tracking the copied byte count: if the returned bytes are smaller then the requested bytes, you are at the end. Alternatively you can use the bool() operator or check the available() bytes from MemoryStream! Please have a look at the class documentation! |
Beta Was this translation helpful? Give feedback.
The connections stays open as long as you return data: It is your choice if you want to return any pcm data or just silence data (an array with 0).
You can easily track if the full MemoryStream has been played by tracking the copied byte count: if the returned bytes are smaller then the requested bytes, you are at the end. Alternatively you can use the bool() operator or check the available() bytes from MemoryStream!
Please have a look at the class documentation!