Is it possible to play a certain Soundfile depending on Events in my Program? #882
-
I've read all your Blog Entires about mp3 Streaming and File Players, but yet - even in Code Snippets from other People here in the Discussions Section - it's always so that all Files from a Directory (SD-FAT or SPIFFS or an Array of URLs) are all grabbed at the same Time and played once one after another during the Program Run. I haven't found a single Example on how to (Pre-) Load a mp3 File, and play it once a certain Condition in my Program is met. Ideally with very low Latency. I am programming some Kind of Game with the ESP32, where I want to give the Players audible Feedback from the Game, such as "you won" or "you lost". I don't need final Example Code, but only some Hints if what I want to achieve is possible with the Library, and where I may get more INformations. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 7 replies
-
I don't really understand where you see any issue: It should be pretty simple to change the example to play one file to playing any file triggered by any event. To keep things simple you could try to use copier.copyAll() instread of running individual copier.copy() steps in the loop. |
Beta Was this translation helpful? Give feedback.
-
You should not pick the most challenging szenario as your first project: try to learn to walk first before you try to fly! Try to start with some simple scenarios and test each of your functionality seperatly: otherwise you have no chance to find what you are doing wrong: ps. you messed up pretty badly with the audioFile: It should be a global variable but you have it as global which is not used because of your local File audioFile definition. I also recommend to use the basic API using callbacks for this scenario. |
Beta Was this translation helpful? Give feedback.
-
I think I have already answered this question above, but I try again: The simplest approach would be to move all functionality that you currently call in setup() into a separate method and at the end just call copier.copyAll(): this whis would then be the method that you can call to play a file. Just change the file name into a method parameter and you should have all you need... If you want to keep the logic with the individual small copy() steps in the loop, you will need to implement some complex state management to figure out in what state the application is. |
Beta Was this translation helpful? Give feedback.
-
Sorry, I overlooked that you want to do this with A2DP: The best way is still, as mentioned, to use basic API. You would just need to make sure that you return an array of 0 if you don't want to send any sound. This way the connection needs to be set up only once. I guess this approach should also work using the A2DPStream and writeSilence(). |
Beta Was this translation helpful? Give feedback.
-
I suggest that you have a look at this example: https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-audiokit/streams-synth-a2dp/streams-synth-a2dp.ino |
Beta Was this translation helpful? Give feedback.
I don't really understand where you see any issue: It should be pretty simple to change the example to play one file to playing any file triggered by any event. To keep things simple you could try to use copier.copyAll() instread of running individual copier.copy() steps in the loop.
What have you tried and where did you fail ?