Search code examples
androidtimerfrequencyaudiotrack

Android play sound until explicitly stopped


Somebody has already written code for generation a tone with a particular frequency for a given time. The SO link is given here: How to Generate A Particular Sound Frequency?.

What I need is, to play the sound indefinitely and not for 3 seconds. I want the sound to stop playing when a button is clicked or something like that. I was thinking maybe I can use timer function to repeat the same loop over and over. When the user clicks 'stop' button I want to invoke audiotrack.pause or stop. Is this a good way to go about it or is there a better logic for this?


Solution

  • Thanks Boggartfly but I found a much simpler solution. I can simply loop the audiotrack object.

    I used the following code.

             audioTrack.setLoopPoints(0, generatedSnd.length/4, -1);
    

    The start frame is zero. The end frame is length/4 for 16bit PCM. The negative 1 here means loop infinite times.