We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there. So I'm still quite new to the coding game and I've been working on a visual code which is going really well. I'm now trying to add some sound. Using Minim audio samples I've made it so a sound plays when you click. However, I would like it to be so that when I click it plays a random 1 of, say, 5 audio samples. How do I go about doing this?
Many thanks in advance for any help
Answers
@AlfieChillar: you can do that easily with minim (or other audio lib). Example (change the name of mp3 to yours!):::
This is so almost perfect! Thank you so much! But how do I now change it so that it plays an audio file on mouseClick instead of back to back on start up?
@AlfieChillar:: in order to do that (i am not sure to understand exactly what you want but i think you can easily adapt...) 1) add some boolean "boolean initPlayer = false" 2) in draw() before if (player.isPlaying() == false) add your conditionnal: if(mousePressed== true){ dont forget to close it with } at the end!!! 3) change the value of the boolean:: initPlayer = true;
Then add your mousePressed() method (after draw, of course)==
---So you can launch or stop the random sound...
I still can't quite get that bit working, but I am closer. Just to try and re-describe what I am aiming for: When you click the mouse it plays a single audio sample, but as oppose to setting just one sample, I would like it to randomly pick one from a few I have.
@AlfieChillar== so, as long as you pressed the mouse the sound will be played...
or if you prefer only clicking:
These are awesome! Thank you so much :) Do you know how I could make it so it just plays one single file when I click? As opposed to starting a string of them?
only one??? not random??? ---- each time in the code you see "son" (String son...) you change the random method to the name of your file; as for an example instead of String son = tableau[zardSon]; player = minim.loadFile(son); you put String son = "mymp3.mp3"; player = minim.loadFile("mymp3.mp3") or you put only one mp3 in the array and leave the code as it is!
But then it will play only one set file. I want it to play one file but for that file to be different/random each time you click.
if you want that the random method NEVER returns a file that was already played you have to add another String[] playedFiles array (same length as the first one) and add to it the played files;( playedFiles.add[son]); then, also in the random method you add a conditionnal "if the file is present into the playedFiles, random again; it' s need a for(...){} loop with an int var which is = to the number of played file; for that you create a global int nbrePlayed files initialized to 0 at the beginning, and incremented each time a new file is played....Of course you must be aware that at some moment ALL the files have to be played, this will happen when your global nbrePlayedFiles == the length of your array of mp3...
Hello ! Thanks a lot for this useful explanation. I'm not really good with english but I think your last explanation (akenaton) is exactly what I want to do with my file. But I don't really understand your explanation, do you have the exempe with the code? Thanks a lot !
A slightly different approach using StringList and its associated method shuffle() to generate a random list where all its items is played at least once before a new shuffled list is generated.
Kf